日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Fliptile(状压+思维)

發(fā)布時(shí)間:2023/12/15 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fliptile(状压+思维) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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
思路:不知道為啥這道題目會(huì)放在搜索專題上,可能搜索的方法也可以解決吧。
首先我們看數(shù)據(jù)量,最大是15.狀壓的一個(gè)很明顯的特點(diǎn)就是數(shù)據(jù)量小。我們?nèi)绻凑毡┝Φ姆椒ǖ脑?#xff0c;數(shù)據(jù)量就特別大,肯定會(huì)超時(shí)。那么我們就將第一行是否翻轉(zhuǎn)的情況枚舉一下。
假如是m列的話,我們枚舉0~(2^m-1)的數(shù)字,轉(zhuǎn)換成二進(jìn)制,就是01串。vis[i][j]=0就是不翻轉(zhuǎn),vis[i][j]=1代表翻轉(zhuǎn)。
我們枚舉了第一行的翻轉(zhuǎn)情況,然后從第二行開(kāi)始,如果該位置的上一個(gè)位置是黑色的話,這一個(gè)位置,是必須要翻轉(zhuǎn)的。翻轉(zhuǎn)完成之后,如果最后一行全是白色的話,就說(shuō)明第一行的翻轉(zhuǎn)是可行的,記錄一下最小值。
題目要求字典序最小,我們枚舉的順序正好滿足了字典序最小。

這是轉(zhuǎn)換成二進(jìn)制串的代碼,結(jié)果如圖所示:

代碼如下:

#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define ll long long #define inf 0x3f3f3f3f using namespace std;const int maxx=16; int a[maxx][maxx];//原數(shù)組 int vis[maxx][maxx];//翻轉(zhuǎn)數(shù)組 int ans[maxx][maxx];//答案數(shù)組 int d[][2]={{1,0},{0,1},{-1,0},{0,-1},{0,0}};//上下左右中 int n,m;bool judge(int x,int y)//判斷一個(gè)點(diǎn)的顏色,是這個(gè)點(diǎn)本身的顏色+翻轉(zhuǎn)的次數(shù);如果本身是白色的話,翻轉(zhuǎn)奇數(shù)次是黑色;如果本身是黑色的話,翻轉(zhuǎn)偶數(shù)次是黑色。這兩種情況加起來(lái)都是奇數(shù)。 {int as=a[x][y];//本身的顏色for(int i=0;i<5;i++){int tx=x+d[i][0];int ty=y+d[i][1];if(tx<0||tx>=n||ty<0||ty>=m) continue;as+=vis[tx][ty];//翻轉(zhuǎn)的次數(shù)}return as&1;//判斷是否為奇數(shù) } inline int solve() {for(int i=1;i<n;i++)for(int j=0;j<m;j++) if(judge(i-1,j)) vis[i][j]=1;//判斷上一個(gè)點(diǎn)是否為黑色,如果是黑色,這個(gè)點(diǎn)必須要翻轉(zhuǎn)for(int i=0;i<m;i++) if(judge(n-1,i)) return -1;//最后一行如果有黑色的話,就說(shuō)明這個(gè)情況不符合。int num=0;for(int i=0;i<n;i++) for(int j=0;j<m;j++) num+=vis[i][j];//記錄一下翻轉(zhuǎn)的次數(shù)return num; } int main() {while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;i++){for(int j=0;j<m;j++) scanf("%d",&a[i][j]);}int flag=0,num;int Ans=inf;for(int i=0;i<(1<<m);i++){memset(vis,0,sizeof(vis));for(int j=0;j<m;j++) vis[0][m-j-1]=(i>>j)&1;//轉(zhuǎn)換成二進(jìn)制串num=solve();if(num!=-1){flag=1;if(num<Ans){Ans=num;for(int o=0;o<n;o++) {for(int p=0;p<m;p++) ans[o][p]=vis[o][p];}}}}if(!flag) cout<<"IMPOSSIBLE"<<endl;else {for(int i=0;i<n;i++){for(int j=0;j<m;j++) cout<<ans[i][j]<<" ";cout<<endl;}}}return 0; }

努力加油a啊

總結(jié)

以上是生活随笔為你收集整理的Fliptile(状压+思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。