南邮 OJ 1613 Counting square
2015-08-06 10:03
309 查看
Counting square
时间限制(普通/Java) : 3000 MS/ 9000 MS 运行内存限制 : 65536 KByte总提交 : 16 测试通过 : 11
比赛描述
There is a matrix of size R rows by C columns. Each element in the matrix is either ‘0’ or ‘1’. A square is called magic square if it meets the following three conditions.
1.The elements on the four borders are all ‘1’.
2.Inside the square (excluding the elements on the borders), the number of 1's and the number of 0's are different at most by 1.
3.The size of the square is at least 2 by 2. Now given the matrix, please tell me how many magic square are there in the matrix.
输入
The input begins with a line containing an integer T , the number of test cases. Each case begins with two integers R , C (1 ≤ R, C ≤ 300) , representing
the size of the matrix. Then R lines follow. Each contains C integers, either ‘0’ or ‘1’. The integers are separated by a single space.
输出
For each case, output the number of magic square in a single line.
样例输入
3
4 4
1 1 1 1
1 0 1 1
1 1 0 1
1 1 1 1
5 5
1 0 1 1 1
1 0 1 0 1
1 1 0 1 1
1 0 0 1 1
1 1 1 1 1
2 2
1 1
1 1
样例输出
3
2
1
题目来源
ICPC
/* AC 1625MS #include<iostream> #define N 301 int a ; int s ; // 矩形(a,b)-(c,d)之间的1的个数,矩形左上角的坐标为(a,b),右下角坐标为(c,d)。 int oneNum(int a,int b,int c,int d){ a--; b--; return s[c][d]-s[a][d]-s[c][b]+s[a][b]; } int main(){ // freopen("test.txt","r",stdin); int T,R,C,i,j,k,count,temp,area; scanf("%d",&T); while(T--){ scanf("%d%d",&R,&C); for(i=1;i<=R;i++){ for(j=1;j<=C;j++){ scanf("%d",&a[i][j]); s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j]; } } count = 0; for(i=1;i+1<=R;i++){ for(j=1;j+1<=C;j++){ for(k=1;i+k<=R && j+k<=C; k++){ if( oneNum(i,j,i,j+k)==k+1 && oneNum(i,j,i+k,j)==k+1 && oneNum(i+k,j,i+k,j+k)==k+1 && oneNum(i,j+k,i+k,j+k)==k+1){ if(k==1 || k==2){ count++; }else{ temp = oneNum(i+1,j+1,i+k-1,j+k-1); area = (k-1)*(k-1); if(temp==area-temp || temp+1==area-temp || temp==area-temp+1){ count++; } } } } } } printf("%d\n",count); } } */ #include<iostream> #define N 301 int a ; int s ; // 矩形(a,b)-(c,d)之间的1的个数,矩形左上角的坐标为(a,b),右下角坐标为(c,d)。 int oneNum(int a,int b,int c,int d){ a--; b--; return s[c][d]-s[a][d]-s[c][b]+s[a][b]; } int main(){ // freopen("test.txt","r",stdin); int T,R,C,i,j,k,count,temp,area; scanf("%d",&T); while(T--){ scanf("%d%d",&R,&C); for(i=1;i<=R;i++){ for(j=1;j<=C;j++){ scanf("%d",&a[i][j]); s[i][j] = s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j]; } } count = 0; for(i=1;i+1<=R;i++){ for(j=1;j+1<=C;j++){ for(k=1;i+k<=R && j+k<=C; k++){ if( oneNum(i,j,i,j+k)==k+1 && oneNum(i,j,i+k,j)==k+1 && oneNum(i+k,j,i+k,j+k)==k+1 && oneNum(i,j+k,i+k,j+k)==k+1){ if(k==1 || k==2){ count++; }else{ temp = oneNum(i+1,j+1,i+k-1,j+k-1); area = (k-1)*(k-1); if(temp==area-temp || temp+1==area-temp || temp==area-temp+1){ count++; } } } } } } printf("%d\n",count); } }
相关文章推荐
- 机器学习经典书籍
- HDU 1010 && ZOJ 2110--Tempter of the Bone【DFS && 奇偶剪枝】
- 解释Activity的四种launchMode模式
- SQL SERVER数据库备份时出现“操作系统错误5(拒绝访问)。BACKUP DATABASE 正在异常终止。”错误的解决办法
- Hdu 5347 MZL's chemistry 2015ACM多校对抗赛第五场
- KeyPress 和KeyDown 、KeyPress的区别
- parseSdkContent failed java.lang.NullPointerException可能与avd相关
- 社説 20150806 原爆忌 被爆地訪問で核軍縮に弾みを
- 南邮 OJ 1608 Slides
- 读书笔记——《程序员自我修养》——编译和链接
- 浅谈HTTP中Get与Post的区别
- Android 中 Handler 引起的内存泄露
- MWPhotoBrowser使用
- Atomic Builtins - Using the GNU Compiler Collection (GCC) GCC 提供的原子操作
- 使用MySQL命令行修改密码
- ExtJs中组件最好少使用ID属性(推荐更多使用Name属性)
- 线程安全的单例模式
- 社説 20150806 米の盗聴疑惑 同盟の信頼関係を揺るがすな
- 南邮 OJ 1601 数次统计
- cleaning selected projects has encountered a problem errors occurred during build