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 }

 

 
 
全部评论

相关推荐

从大一开始就开始学习Java,一路走来真的不算容易,每次面试都被压力,不过这次终于达成了自己的一大心愿!时间线和面经:8.17-投递9.1-一面实习+项目拷打看门狗机制讲一下redis加锁解锁的本身操作是什么Lua脚本是干什么的udp和tcp讲一下流量控制讲一下令牌桶算法说一下大端和小端是什么线程和协程有什么区别怎么切换协程切换的时候具体做了什么对于程序来说,你刚才提到的保存和恢复现场,这个现场有哪些信息udp优势现在有一个客户端和服务端,要实现TCP的通信,我们的代码要怎么写服务器怎么感知有新的连接怎么处理多个客户端的请求连接TCP怎么处理粘包和分包现在有两个文件,然后每个文件都有一亿条URL,每个的长度都很长,要怎么快速查找这两个文件共有的URLHashmap底层说一下怎么尽量提升插入和查询的效率如果要查找快,查询快,还有解决非空的问题,怎么做LoadingCache了解吗手撕:堆排序9.4-二面部门的leader,超级压力面拷打实习+项目,被喷完全没东西类的加载到垃圾回收整个底层原理讲一遍类加载谁来执行类加载器是什么东西,和进程的关系Java虚拟机是什么东西,和进程的关系如果我们要执行hello&nbsp;world,那虚拟机干了什么呢谁把字节码翻译成机器码,操作时机是什么Java虚拟机是一个执行单元吗Java虚拟机和操作系统的关系到底什么,假如我是个完全不懂技术的人,举例说明让我明白一个操作系统有两个Java程序的话,有几个虚拟机有没有单独的JVM进程存在启动一个hello&nbsp;world编译的时候,有几个进程JVM什么时候启动比如执行一条Java命令的时候对应一个进程,然后这个JVM虚拟机到底是不是在这个进程里面,还是说要先启动一个JVM虚拟机的进程垃圾回收机制的时机能手动触发垃圾回收吗垃圾回收会抢占业务代码的CPU吗垃圾回收算法简单说说垃圾回收机制的stop&nbsp;the&nbsp;world存在于哪些时机垃圾回收中的计算Region的时候怎么和业务代码并行执行假如只有一个线程,怎么实现并行Java为什么要这么实现Java效率比C++慢很多,那为什么还要这样实现Java虚拟机到底是什么形式存在的说一下Java和C++的区别还有你对Java设计理念的理解无手撕面试结束的时候,我真的汗流浃背了,面试官还和我道歉,说他是故意压力面想看看我的反应的,还对我给予了高度评价:我当面试官这么多年,你是我见过最好的一个9.9-三面临时通知的加面,就问了三十分钟项目9.11-hr面问过往经历,未来计划,想从腾讯实习中得到什么?当场告知leader十分满意我,所以直接ochr面完一分钟官网流程变成录用评估中,30分钟后mt加微信告知offer正在审批9.15-offer这一次腾讯面试体验真的不错,每个面试官能感觉到专业能力很强,反馈很足,比起隔壁某节真是好太多以后就是鹅孝子了
三本咋了:当面试官这么多年你是我见过的最好的一个
你面试被问到过哪些不会的...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务