Advertisement
Saleh127

UVA 639

Aug 11th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n;
  4. int xx[5][5];
  5. bool valid(int x,int y)
  6. {
  7. bool s=true;
  8. int i,j;
  9. for(i=1;i<=n;i++)
  10. {
  11. if(i==x) continue;
  12. if(xx[i][y]==2)
  13. {
  14. s=s&false;
  15. break;
  16. }
  17. if(xx[i][y]==1)
  18. {
  19. s=s&true;
  20. break;
  21. }
  22. }
  23. for(j=1;j<=n;j++)
  24. {
  25. if(j==y) continue;
  26. if(xx[x][j]==2)
  27. {
  28. s=s&false;
  29. break;
  30. }
  31. if(xx[x][j]==1)
  32. {
  33. s=s&true;
  34. break;
  35. }
  36. }
  37. return s;
  38. }
  39.  
  40. int backtrack()
  41. {
  42. int maxx=0,ans,i,j;
  43. for(i=1;i<=n;i++)
  44. {
  45. for(j=1;j<=n;j++)
  46. {
  47. if(xx[i][j]==0 && valid(i,j))
  48. {
  49. xx[i][j]=2;
  50. ans=1+backtrack();
  51. maxx=max(maxx,ans);
  52. xx[i][j]=0;
  53. }
  54. }
  55. }
  56. return maxx;
  57. }
  58.  
  59. int main()
  60. {
  61. ios_base::sync_with_stdio(0);
  62. cin.tie(0);cout.tie(0);
  63.  
  64. char ss;
  65. int i,j;
  66. while(cin>>n && n)
  67. {
  68.  
  69. for(i=1;i<=n;i++)
  70. {
  71. for(j=1;j<=n;j++)
  72. {
  73. cin>>ss;
  74. if(ss=='.') xx[i][j]=0;
  75. else xx[i][j]=1;
  76. }
  77. }
  78. cout<<backtrack()<<endl;
  79. }
  80.  
  81. return 0;
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement