Fliptile
Time Limit:2000MS
Memory Limit:65536KB
64bit IO Format:%lld & %llu
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 anM ×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 41 0 0 10 1 1 00 1 1 01 0 0 1
Sample Output
0 0 0 01 0 0 11 0 0 10 0 0 0
這道題題意:有一個n*m的格子,每個格子都有黑白兩面(0表示白色,1表示黑色)。每反轉一個格子,它上下左右的格子都會跟著反轉。我們需要把所有的格子都反轉成白色,請用最小步數完成,並輸出反轉時每個格子反轉的次數。有多個解時,輸出字典序最小的一組。
做這題之前,我們首先需要理解一個現象:除了最後一行,其他任何一行上的1都可以通過下一行的翻轉轉換成0。也就是說,除了最後一行外,我們總是可以通過翻轉,將前n-1行翻成全0。只要按照這樣的原則:對於某一個位置x,如果它的上一個是1,就翻轉它,如果是0,就不翻轉。知道了這個,我們應該就能想到,第一行的翻法直接就決定了後面的所有的翻法,這就是我們解決這道題目的思路,即枚舉第一行所有可能的翻法,這裡,如果用循來枚舉,未免太浪費時間了,所以,這裡採取了一種二進位壓縮的方法。
首先讓我們來學習一下左移<<的概念,其實很好理解,就是將一個數轉換為二進位,然後向左移動若干位,然後在多出來的位置上補零,比如,對於5<<2,5的二進位為101,左移兩位就是10100,那麼最後的結果就是20,。
利用這個特點,我們可以通過使用一個特殊的數字1,來解決這個枚舉問題。枚舉的第一步就是確定到底要改變哪幾位元,聯想到二進位,我們可以這樣處理,就題目的測試資料而言,一行有四個數,用一個位元xxxx表示,易知,xxxx一共有2^4種排列,其實也是1<<4,之所以這樣寫,是因為這比pow要快,所以,我們讓k從0開始枚舉到15,也就是0000到1111,然後規定,只要是帶1的位置,就要翻轉這個位置的數字,問題又來了,怎麼知道哪一位是1,呢,這裡還是用到了二進位,即與運算,我們讓k分別與1000,0100,0010,0001進行與運算,分別對應不同的位,如果結果不是1,說明這一位上不是0,是不是很巧妙,當然,那四個值依然是通過1的左移來計算出來的,核心的東西講完了,下面上代碼:
#include<stdio.h>#include<string.h>int M,N;int map[20][20];int vis[20][20]; //儲存每一格的翻轉次數int t[20][20]; //臨時數組用來儲存中間結果int d[4][2]={0,-1,-1,0,0,1,1,0};int cnt;void Flip(int i,int j){ //翻轉cnt++; //次數加1t[i][j]=!t[i][j]; //先翻轉自己vis[i][j]=1;for(int k=0;k<4;k++){ //翻轉上下左右四個方向的格子int x=i+d[k][0];int y=j+d[k][1];if(x<0||y<0||x>=N||y>=M) //越界判斷continue;t[x][y]=!t[x][y]; //翻轉}}bool Judge(int num){ //對於第一行的每一種翻轉情況,判斷是否能產生矩陣全翻轉為0的結果int i,j;cnt=0; memcpy(t,map,sizeof(t)); //初始化臨時數組for(i=0;i<M;i++){ //這裡採用了二進位壓縮,例如,j從0到3,那麼1 << i的二進位就是0001,0010,0100,1000if(num&(1<<i)){ //對於num對應的位元為1的每一位,都應該翻轉Flip(0,i); //如果某一位為1,就翻轉第一行的這個位置}}for(i=0;i<N-1;i++){ //第一行翻轉之後,第二行到倒數第二行的翻轉(按上面我說的原則去翻轉)for(j=0;j<M;j++){if(t[i][j]==1)Flip(i+1,j);}}for(i=0;i<M;i++){ //因為每一行的1都可以由下一行搞定,但是最後一行沒有下一行,所以只需要考察最後一行最後是不是全0就可以了if(t[N-1][i])return false;}return true;}int main(){int i,j;while(scanf("%d %d",&N,&M)!=EOF){for(i=0;i<N;i++){for(j=0;j<M;j++){scanf("%d",&map[i][j]);}}int step=N*M+1; //初始化 int p=-1; //step存最少的翻轉步數,p存第幾次的翻轉步數最少for(i=0;i<(1<<M);i++){ //i表示一個位元,用來枚舉第一行的各種不同翻法,如0001就是只翻最後一個if(Judge(i)&&cnt<step){ //如果找到一種可能並且所用的步數更少的話,記下這種翻法 step=cnt;p=i;}}memset(vis,0,sizeof(vis)); if(p>=0){ //最後找到的就是最少的翻法,類比一遍,然後輸出 Judge(p);for(i=0;i<N;i++){for(j=0;j<M;j++){if(j==M-1)printf("%d\n",vis[i][j]);else printf("%d ",vis[i][j]);}}}else printf("IMPOSSIBLE\n"); //不可能使矩陣全翻轉為0}return 0;}