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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Fliptile——搜索+二进制优化

發(fā)布時(shí)間:2023/11/30 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fliptile——搜索+二进制优化 小編覺得挺不錯(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

【題目分析】
看完題目后很明顯應(yīng)該是一個(gè)暴力搜索,但是應(yīng)該從哪里開始搜索呢?從頭開始的話會(huì)有215*15種可能,顯然是不允許的。我們應(yīng)該優(yōu)化一下搜索的順序,即哪些情況是顯然不可能成立的。
我們?nèi)绻麖纳贤滤阉?#xff0c;那么下面的對(duì)上面的影響只有四個(gè)方向中上面的那個(gè),那么如果搜索在x行,那么x-1行中的1下面的那塊必須翻轉(zhuǎn),0下面的那塊不能翻轉(zhuǎn)。這樣類推,我們發(fā)現(xiàn)除了第一行其他行的翻轉(zhuǎn)情況都已經(jīng)被確定,因此我們只需要枚舉第一行的翻轉(zhuǎn)情況(215種)就可以了。
看大佬的博客發(fā)現(xiàn)用到一種二進(jìn)制的優(yōu)化。如果沒有這種優(yōu)化的話我們可能需要搜索,相對(duì)比較復(fù)雜。
我們可以將一行中翻轉(zhuǎn)的情況看做一個(gè)數(shù),例如:110就是翻轉(zhuǎn)前兩個(gè),那么總共的情況就是1<<m種
對(duì)于一個(gè)數(shù)字,我們?nèi)绾慰焖倥袛嗄男┪皇?呢?
我們可以用110與100,010,001的與判斷,如果與的結(jié)果是0則該位是0,如果結(jié)果不是0則該位是1
有上面的方法后我們就可以寫出程序啦
【AC代碼】(借鑒大佬)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<climits> #include<string> #include<queue> using namespace std;int n,m,num,cnt,ans; const int MAXN=20; int a[MAXN][MAXN]; //原數(shù)組 int b[MAXN][MAXN]; //用來修改的臨時(shí)數(shù)組 int c[MAXN][MAXN]; //保存答案 const int drc[5][2]={0,0,0,1,0,-1,1,0,-1,0};void rev(int i,int j) {int u,v;++cnt; c[i][j]=1; //記錄答案for(int k=0;k<5;k++){u=i+drc[k][0]; v=j+drc[k][1];if(u<0 || u>=n || v<0 || v>=m) continue;b[u][v]^=1; //翻轉(zhuǎn)} }bool check(int x) {cnt=0;memcpy(b,a,sizeof(b)); //直接將a拷貝給bfor(int i=0;i<m;i++){if(x&(1<<(m-1-i)))//1<<(m-1-i)是第i位為1其他位為0的數(shù),用來判斷x在第i位是不是1rev(0,i); //如果是1就翻轉(zhuǎn)}for(int i=1;i<n;i++){for(int j=0;j<m;j++){if(b[i-1][j]) rev(i,j); //如果上一行是1就翻轉(zhuǎn)}}for(int i=0;i<m;i++){if(b[n-1][i]) return false; //前面肯定都是0了,只需要判斷最后一行是不是0就可以了}return true; }int main() {scanf("%d%d",&n,&m);ans=INT_MAX; num=-1;memset(a,0,sizeof(a));for(int i=0;i<n;i++){for(int j=0;j<m;j++){scanf("%d",&a[i][j]);}}for(int i=0,j=1<<m;i<j;i++){if(check(i) && cnt<ans) //這樣記錄的答案肯定是字典序,前面的能不翻就不翻,盡量翻后面的{ans=cnt; num=i;}}memset(c,0,sizeof(c)); //用來保存答案,之前都不用保存if(num!=-1){check(num);for(int i=0;i<n;i++){for(int j=0;j<m-1;j++){printf("%d ",c[i][j]);}printf("%d\n",c[i][m-1]);}}else{printf("IMPOSSIBLE");}return 0; }

總結(jié)

以上是生活随笔為你收集整理的Fliptile——搜索+二进制优化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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