As input, you are given a (complete or incomplete) n*n grid made with no more than 2n(n+1) matchsticks for a natural number 5 <= n . Your task is to compute the minimum number of matchsticks taken《Square Destroy》来自:这里
out to destroy all the squares existing in the input n*n grid.
今天,不 开 心 ,英语好弱,居然读错题了,题目在上边。
我是这样读的:用木条拼成n*n个相邻的小正方形,破坏m根,还剩?个正方形。
正确的是:.......破坏m根,那么,还得破坏?根才能使正方形不存在。
按自己思路编的代码还在debug中……
#include<stdio.h> int a[10][10]; int main() { int check(int x, int y, int n); int i,j,k,s,n,d; scanf("%d",&n); for (i=1; i<=n; i++) for (j=1; j<=n; j++) a[i][j]=1; for (i=1; i<=n; i++) for (j=1; j<=n; j++) a[2*i][2*j]=0; /*初始化数组a*/ scanf("%d",&d); for (i=0; i<d; i++) { int dd,mo,di; scanf("%d",&dd); mo=dd%(2*n+1); di=dd/(2*n+1); if (mo>n) a[2*di+2][2*mo-1]=0; else a[2*di+1][2*mo]=0; } for (i=2,s=0; i<=2*n; i=i+2) for (j=2; j<=2*n; j=j+2) for (k=1; k<=n; k++) s+=check(i,j,k); printf("%d\n",s); } int check(int x, int y, int n) { int i,ok; for (i=0,ok=1; i<n && ok; i++) { if (!a[x-1][y+2*i]) ok=0; if (!a[x+1+2*i][y+2*i]) ok=0; if (!a[x+2*i][y-1]) ok=0; if (!a[x+2*i][y+1+2*i]) ok=0; } return(ok); }