POJ - 3279(枚举+暴力)

Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14297   Accepted: 5257

Description

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".

Input

Line 1: Two space-separated integers:  M and  N 
Lines 2.. M+1: Line  i+1 describes the colors (left to right) of row i of the grid with  N space-separated integers which are 1 for black and 0 for white

Output

Lines 1.. M: Each line contains  N space-separated integers, each specifying how many times to flip that particular location.

Sample Input

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

Sample Output

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

Source

 
时隔一年,终于又开始A题了。。。
又要重新拾起来,好多东西都忘了。。。
 
题意:有一个m*n的棋盘,每一个棋子都是一面黑色(1表示),一面白色(0表示),每次翻一颗棋子时,他的上下左右四个也随着翻过来,目标是翻成全是白色的,问用最少的次数达到目标时,都是要翻哪些棋子。
题目归到了搜索里面,想了好久也没想出来。
 
因为棋子只有黑色和 白色,所以翻一次时会变成另一个颜色,翻两次就变回原来的颜色了,所以每个棋子都可以看作只有翻和不翻两种状态。
最暴力的想法是枚举m*n个棋子的状态,每一个棋子都是有两种状态,时间复杂度就是2^(m*n),这样太大了,肯定不行。
看了几个题解,都是写了思路,但是具体为什么这样做好像解释的不太清楚。
思路:因为翻每一个棋子,都会导致他的上下左右的棋子变化,如果想让1变成0,可以翻他的旁边的棋子来达到目的,(统一按照翻它下方的棋子),如果从第一行往下赶,赶到最后一行,上面的m-1行肯定都是0了,所以只需要解决最后一行就行了,而最后一行最多15位,枚举就行了,时间复杂度是2^15。
枚举是借助二进制,00001代表只翻最后一个,+1之后是00010,代表翻倒数第二个。。。
我写的代码是先枚举第一行,然后往下赶,再看最后一行是不是全是0.
 
 
 
 
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 
  5 using namespace std;
  6 
  7 int m,n;
  8 
  9 bool right(int i,int j){
 10     if(i>=0&&i<m&&j>=0&&j<n){
 11         return true;
 12     }else{
 13         return false;
 14     }
 15 }
 16 
 17 void flip(char grid[][16],int i,int j){
 18     int ii=i,jj=j;
 19 
 20     if(grid[ii][jj]=='1'){
 21         grid[ii][jj]='0';
 22     }else{
 23         grid[ii][jj]='1';
 24     }
 25 
 26     ii=i+1,jj=j;
 27     if(right(ii,jj)){
 28         if(grid[ii][jj]=='1'){
 29             grid[ii][jj]='0';
 30         }else{
 31             grid[ii][jj]='1';
 32         }
 33     }
 34 
 35     ii=i-1,jj=j;
 36     if(right(ii,jj)){
 37         if(grid[ii][jj]=='1'){
 38             grid[ii][jj]='0';
 39         }else{
 40             grid[ii][jj]='1';
 41         }
 42     }
 43 
 44     ii=i,jj=j+1;
 45     if(right(ii,jj)){
 46         if(grid[ii][jj]=='1'){
 47             grid[ii][jj]='0';
 48         }else{
 49             grid[ii][jj]='1';
 50         }
 51     }
 52 
 53     ii=i,jj=j-1;
 54     if(right(ii,jj)){
 55         if(grid[ii][jj]=='1'){
 56             grid[ii][jj]='0';
 57         }else{
 58             grid[ii][jj]='1';
 59         }
 60     }
 61 
 62 }
 63 
 64 int main()
 65 {
 66     char reserve[16][16];
 67     char grid[16][16];
 68     char result[16][16];
 69     char output[16][16];
 70     int bin[16];
 71     scanf("%d %d",&m,&n);
 72     getchar();
 73     for(int i=0;i<m;i++){
 74         for(int j=0;j<n;j++){
 75             scanf("%c",&reserve[i][j]);
 76             getchar();
 77         }
 78     }
 79     int coun=1<<n;
 80 
 81     int mincou=99999;
 82     for(int iii=0;iii<coun;iii++){
 83 
 84         memcpy(grid,reserve,sizeof(reserve));
 85         memset(result,'0',sizeof(result));
 86 
 87         int cou=0;
 88         int t=iii;
 89         int b_i=0;
 90         bin[0]=0;
 91 
 92         while(t){
 93             bin[b_i]=t%2;
 94             t/=2;
 95             result[0][b_i]='0'+bin[b_i];
 96             b_i++;
 97         }
 98         for(int i=0;i<n;i++){
 99             if(result[0][i]=='1'){
100                 cou++;
101                 flip(grid,0,i);
102             }
103         }
104 
105         for(int i=0;i<m-1;i++){
106             for(int j=0;j<n;j++){
107                 if(grid[i][j]=='1'){
108                     flip(grid,i+1,j);
109                     result[i+1][j]='1';
110                     cou++;
111                 }
112             }
113         }
114         bool f=true;
115         for(int i=0;i<n;i++){
116             if(grid[m-1][i]=='1'){
117                 f=false;
118                 break;
119             }
120         }
121         if(f==true){
122             if(cou<mincou){
123                 mincou=cou;
124                 memcpy(output,result,sizeof(result));
125             }else if(cou==mincou){
126                 bool flag=true;
127                 for(int i=0;i<m;i++){
128                     for(int j=0;j<n;j++){
129                         if(output[i][j]>result[i][j]){
130                             break;
131                         }else if(output[i][j]<result[i][j]){
132                             flag=false;
133                             break;
134                         }
135                     }
136                 }
137                 if(flag){
138                     memcpy(output,result,sizeof(result));
139                 }
140             }
141         }
142     }
143 
144     if(mincou==99999){
145         printf("IMPOSSIBLE\n");
146     }else{
147         for(int i=0;i<m;i++){
148             for(int j=0;j<n;j++){
149                 if((i==m-1) && (j==n-1)){
150                     printf("%c",output[i][j]);
151                 }else{
152                     printf("%c ",output[i][j]);
153                 }
154             }
155             if(i!=m-1){
156                 printf("\n");
157             }
158 
159         }
160     }
161 
162 
163     return 0;
164 }

 

 
 
全部评论

相关推荐

点赞 评论 收藏
分享
挣K存W养DOG:他真的很中意你,为什么不回他
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务