1002 Fire Net, 1002 firenet
Recursively implementing enumeration in various situations can be seen as a simple implementation of DPS.
1 #include <stdio.h> 2 3 int n,max,count,target[4][4]; 4 5 int place(int x,int y){ 6 int i; 7 for(i=y;i>=0;i--){ 8 if(target[x][i]>0) 9 return 0;10 if(target[x][i]<0)11 break;12 }13 for(i=y;i<n;i++){14 if(target[x][i]>0)15 return 0;16 if(target[x][i]<0)17 break;18 }19 for(i=x;i>=0;i--){20 if(target[i][y]>0)21 return 0;22 if(target[i][y]<0)23 break;24 }25 for(i=x;i<n;i++){26 if(target[i][y]>0)27 return 0;28 if(target[i][y]<0)29 break;30 }31 return 1;32 }33 34 void DFS(){35 int i,j;36 if(count>max)37 max=count;38 for(i=0;i<n;i++){39 for(j=0;j<n;j++){40 if(!target[i][j]&&place(i,j)){41 target[i][j]=1;42 count++;43 DFS();44 count--;45 target[i][j]=0;46 }47 }48 }49 }50 51 int main(){52 int i,j;53 char c;54 while(1){55 scanf("%d",&n);56 if(n==0)57 break;58 max=count=0;59 for(i=0;i<n;i++){60 getchar();61 for(j=0;j<n;j++){62 scanf("%c",&c);63 target[i][j]=(c=='X'?-1:0);64 }65 }66 DFS();67 printf("%d\n",max);68 }69 return 0;70 }
ACM's OJ question 1002: fire net helped the local test to be correct, but the submission always failed.
For your reference
# Include <iostream>
# Include <cstring>
Using namespace std;
Char map [4] [4];
Int maxt;
Bool legal (int a, int B)
{
Int I;
If (map [a] [B] = 'X' | map [a] [B] = '+') return false;
For (I = A-1; I> = 0; I --)
{
If (map [I] [B] = '+') return false;
Else if (map [I] [B] = 'X') break;
}
For (I = B-1; I> = 0; I --)
{
If (map [a] [I] = '+') return false;
Else if (map [a] [I] = 'X') break;
}
Return true;
}
Void maker (int k, int count, int n)
{
Int I, j;
If (k = n * n)
{
If (count> maxt) maxt = count;
Return;
}
Else
{
I = k/n;
J = k % n;
If (legal (I, j ))
{
Map [I] [j] = '+ ';
Maker (k + 1, count + 1, n );
Map [I] [j] = '.';
}
Maker (k + 1, count, n );
}
}
Int main ()
{
Int n;
While (cin> n)
{
If (n = 0) break;
Int I, j;
For (I = 0; I <n; I ++) for (j = 0; j <n; j ++) cin> map [I] [j];
Maxt = 0;
Maker (0, 0, n );
Cout <maxt <endl;
}
}
Zoj 1002 fire net question: is there a problem with my thinking?
Because Windows developers define their own CHAR and TCHAR, and they define their own CHAR as unsigned char. To prevent different compilers from generating different code, because the C Standard does not specify whether char must be unsigned. Therefore, it is better to fix it by yourself. In addition, to be compatible with the 8-bit extended ASCII code processing in DOS, it should be 0 ~ The value range is 255. -128 ~ The char of 127 is just the definition used by the C language compiler in the early days. This definition is inherited by Microsoft's C compiler, but the OS developers and compiler teams both want some independence.