POJ 2965 the pilots ' refrigerator

Source: Internet
Author: User
Tags define local

Original title Link: http://poj.org/problem?id=2965

Simple DFS, because it is required minimum value, enumeration depth to get the recursive termination condition, and poj1753 very much like, after all, is put together.

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <string>
  9. #define CLR (arr) memset (arr,0,sizeof(arr))
  10. #define N 100001
  11. #define MOD 1e9+7
  12. //#define LOCAL
  13. typedef LONG long ll;
  14. using namespace std;
  15. int cmp (const void*c,const void*d) {return 1;} //above a lot of basic no eggs, just to use the time no more dozen, anyway compile too long and not suffer
  16. Char handle[4][4];
  17. int h[6][6];
  18. int step,flag,fa[2][50],top;
  19. void Flip (int row,int col) {
  20. for (int i=1;i<=4;i++) h[i][col]^=1;
  21. for (int j=1;j<=4;j++) if (J!=col) H[row][j]^=1; //Make the row flip with the column
  22. return;
  23. }
  24. bool isopen () {
  25. for (int i=1;i<=4;i++)
  26. for (int j=1;j<=4;j++)
  27. if (H[i][j]) return false;
  28. return true; //Determine if it is open
  29. }
  30. void dfs (int row,int col,int deep) {
  31. if (Deep==step) {
  32. if (IsOpen ()) {
  33. printf ("%d\n", step);
  34. for (int i=0;i<top;i++) printf ("%d%d\n", Fa[0][i],fa[1][i]);
  35. flag=1;
  36. }
  37. return;
  38. }
  39. if (Flag| | row==5)return ;
  40. Fa[0][top]=row;
  41. Fa[1][top++]=col; //Save the path, in fact, you can change the DFS to take the int return value, add judgment to save, perhaps also can reduce time
  42. Flip (Row,col);
  43. if (col<4)
  44. DFS (ROW,COL+1,DEEP+1);
  45. Else
  46. DFS (ROW+1,1,DEEP+1);
  47. top--;
  48. Flip (Row,col);
  49. if (col<4)
  50. DFS (ROW,COL+1,DEEP);
  51. Else
  52. DFS (ROW+1,1,DEEP);
  53. return;
  54. }
  55. int Main ()
  56. {
  57. #ifdef LOCAL
  58. Freopen ("input.txt", "R", stdin);
  59. //freopen ("Output.txt", "w", stdout);
  60. #endif
  61. for (int i=0;i<4;i++) {
  62. scanf ("%s", Handle[i]);
  63. for (int j=1;j<=4;j++) h[i+1][j]= (handle[i][j-1]== ' + ');
  64. }
  65. for (Step=1;; step++) {
  66. DFS (1,1,0);
  67. if (flag) break;
  68. }
  69. return 0;
  70. }

I can spit groove I actually do not understand what is the content of the surface? In fact, this water problem is not too shy to put up, but as a souvenir, after all, POJ training first gun.

POJ 2965 the pilots ' refrigerator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.