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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ - 2965 The Pilots Brothers' refrigerator(bfs+路径输出/思维+位运算)

發布時間:2024/4/11 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ - 2965 The Pilots Brothers' refrigerator(bfs+路径输出/思维+位运算) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個4*4的矩陣,每個點都代表一個開關,'+'代表關,'-'代表開,每次操作可以任意改變一個開關(x,y)的狀態,但代價是x行和y列的開關都要一起改變狀態,題目要求將所有的開關都打開,問最少操作次數以及哪些點需要操作

題目分析:思維不夠,暴力來湊,看到這個題目只有4*4=16個格子,也就是一共只有2^16種情況,直接上bfs遍歷所有狀態就好了,可以用一個數字保存狀態矩陣,用二進制優化一下,然后就能無壓力的A掉了

但其實這個題目是個簡單思維題,因為開關兩個狀態的互相轉換,相當于取異或,對于每個初始時為'+'的開關,在其所在的行和列的操作數之和必須為奇數才能將當前開關變為'-',又因為異或操作,若對于一個相同的點操作兩次,相當于沒有操作過,因為互相抵消了,所以我們可以對于每個'+'所在的點的行和列記錄操作次數,并記得對2取模,最后統計一下有多少個點的權值為1,說明這些點需要翻轉一次

代碼:

bfs:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=(1<<16)+100;int vis[N];struct Node {int x,y,state;Node(int X,int Y,int STATE){x=X;y=Y;state=STATE;}Node(){} }pre[N];void bfs(int st) {queue<int>q;q.push(st);vis[st]=true;while(q.size()){int cur=q.front();q.pop();if(!cur)return;for(int i=0;i<16;i++){int x=i/4;int y=i%4;int next=cur^(1<<(x*4+y));for(int j=0;j<4;j++){next^=(1<<(x*4+j));next^=(1<<(j*4+y));}if(vis[next])continue;vis[next]=true;pre[next]=Node(x,y,cur);q.push(next);}} }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int st=0;for(int i=0;i<4;i++)//'+':1 '-':0{ char s[10];scanf("%s",s); for(int j=0;j<4;j++){int pos=i*4+j;if(s[j]=='+')st|=(1<<pos);}}bfs(st);stack<pair<int,int> >s;int k=0;while(k!=st){s.push(make_pair(pre[k].x,pre[k].y));k=pre[k].state;}printf("%d\n",s.size());while(s.size()){printf("%d %d\n",s.top().first+1,s.top().second+1);s.pop();}return 0; }

思維:

#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;int maze[10][10];char s[10][10];int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);for(int i=1;i<=4;i++)scanf("%s",s[i]+1);for(int i=1;i<=4;i++)for(int j=1;j<=4;j++){if(s[i][j]=='+'){maze[i][j]=(maze[i][j]+1)%2;for(int k=1;k<=4;k++){maze[i][k]=(maze[i][k]+1)%2;maze[k][j]=(maze[k][j]+1)%2;}}}int ans=0;for(int i=1;i<=4;i++)for(int j=1;j<=4;j++)if(maze[i][j])ans++;printf("%d\n",ans);for(int i=1;i<=4;i++)for(int j=1;j<=4;j++)if(maze[i][j])printf("%d %d\n",i,j);return 0; }

?

總結

以上是生活随笔為你收集整理的POJ - 2965 The Pilots Brothers' refrigerator(bfs+路径输出/思维+位运算)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。