洛谷 2346 四子连棋 bfs
来源:互联网 发布:手机淘宝秒杀需要刷新 编辑:程序博客网 时间:2024/05/29 04:57
题目:
https://www.luogu.org/problem/show?pid=2346
40分钟打出代码(80分)+5个小时调试=AC
水题,没有任何技巧的bfs,注意状态的储存;
目标状态: 横竖斜4个同色;
but! 由于忘了判竖着的情况,所以一直80,调了5、6个小时?;
洛谷不让下数据……所以只能手算……然后和电脑比较答案……;
不过这个题用来练bfs挺好的
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int mod=131373,base=137;bool vis[2000005];struct hh { int a[8][8],x1,y1,x2,y2,step,up;}star;string s[8];//1 hei ,2 bai;queue<hh>q;int ans,X[6]={0,1,0,-1,0},Y[6]={0,0,-1,0,1},fx,fy;int hashs(hh now){ int tot=0; for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) tot=(base*tot+(int)now.a[i][j])%mod; return tot;}bool pd(hh now){ if(now.a[2][2]==now.a[1][1] && now.a[3][3]==now.a[1][1] && now.a[4][4]==now.a[1][1]) return true; for(int i=1;i<=4;i++) { if(now.a[i][1]==now.a[i][2] && now.a[i][1]==now.a[i][3] && now.a[i][1]==now.a[i][4]) return true; if(now.a[1][i]==now.a[2][i] && now.a[1][i]==now.a[3][i] && now.a[1][i]==now.a[4][i]) return true; } return false;}int bfs(){ memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); q.push(star),vis[hashs(star)]=1; while(!q.empty()) { hh now=q.front(),nxt; q.pop(); if(pd(now)) return now.step; else { for(int i=1;i<=8;i++) { if(i<=4) fx=now.x1+X[i],fy=now.y1+Y[i]; else fx=now.x2+X[i-4],fy=now.y2+Y[i-4]; nxt=now; if(now.a[fx][fy]!=now.up || fx<1 || fx>4 || fy<1 || fy>4) continue; if(i<=4) swap(nxt.a[fx][fy],nxt.a[now.x1][now.y1]); else swap(nxt.a[fx][fy],nxt.a[now.x2][now.y2]); if(!vis[hashs(nxt)]) { if(i<=4) nxt.x1=fx,nxt.y1=fy; else nxt.x2=fx,nxt.y2=fy; vis[(hashs(nxt))]=1; if(now.up & 1) nxt.up=2; else nxt.up=1; nxt.step=now.step+1; q.push(nxt); } } } }}void solve(){ for(int i=1;i<=4;i++) { cin>>s[i]; for(int j=0;j<4;j++) if(s[i][j]=='B') star.a[i][j+1]=1; else if(s[i][j]=='W') star.a[i][j+1]=2; else if(star.x1) star.x2=i,star.y2=j+1; else star.x1=i,star.y1=j+1; } star.up=1,ans=bfs(),star.up=2; cout<<min(bfs(),ans); return;}int main(){ solve(); return 0;}
2017.10.26 感谢Sher_lock指出BUG;
修改后的
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int mod=131373,base=137;bool vis[2000005];struct hh { int a[8][8],x1,y1,x2,y2,step,up;}star;string s[8];//1 hei ,2 bai;queue<hh>q;int ans,X[6]={0,1,0,-1,0},Y[6]={0,0,-1,0,1},fx,fy;int hashs(hh now){ int tot=0; for(int i=1;i<=4;i++) for(int j=1;j<=4;j++) tot=(base*tot+(int)now.a[i][j])%mod; return tot;}bool pd(hh now){ if(now.a[2][2]==now.a[1][1] && now.a[3][3]==now.a[1][1] && now.a[4][4]==now.a[1][1]) return true; if(now.a[1][4]==now.a[2][3] && now.a[3][2]==now.a[1][4] && now.a[4][1]==now.a[1][4]) return true; for(int i=1;i<=4;i++) { if(now.a[i][1]==now.a[i][2] && now.a[i][1]==now.a[i][3] && now.a[i][1]==now.a[i][4]) return true; if(now.a[1][i]==now.a[2][i] && now.a[1][i]==now.a[3][i] && now.a[1][i]==now.a[4][i]) return true; } return false;}int bfs(){ memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); q.push(star),vis[hashs(star)]=1; while(!q.empty()) { hh now=q.front(),nxt; q.pop(); if(pd(now)) return now.step; else { for(int i=1;i<=8;i++) { if(i<=4) fx=now.x1+X[i],fy=now.y1+Y[i]; else fx=now.x2+X[i-4],fy=now.y2+Y[i-4]; nxt=now; if(now.a[fx][fy]!=now.up || fx<1 || fx>4 || fy<1 || fy>4) continue; if(i<=4) swap(nxt.a[fx][fy],nxt.a[now.x1][now.y1]); else swap(nxt.a[fx][fy],nxt.a[now.x2][now.y2]); if(!vis[hashs(nxt)]) { if(i<=4) nxt.x1=fx,nxt.y1=fy; else nxt.x2=fx,nxt.y2=fy; vis[(hashs(nxt))]=1; if(now.up & 1) nxt.up=2; else nxt.up=1; nxt.step=now.step+1; q.push(nxt); } } } }}void solve(){ for(int i=1;i<=4;i++) { cin>>s[i]; for(int j=0;j<4;j++) if(s[i][j]=='B') star.a[i][j+1]=1; else if(s[i][j]=='W') star.a[i][j+1]=2; else if(star.x1) star.x2=i,star.y2=j+1; else star.x1=i,star.y1=j+1; } star.up=1,ans=bfs(),star.up=2; cout<<min(bfs(),ans); return;}int main(){ solve(); return 0;}
阅读全文
1 0
- 洛谷 2346 四子连棋 bfs
- ACM 1004 四子连棋(BFS)
- 1004 四子连棋 (bfs)
- 洛谷p2298 bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- BFS
- BFS
- 一个web项目web.xml的配置中<context-param>配置作用
- FZU1686 神龙的难题 —— Dancing Links 可重复覆盖
- linux下redis与phpredis安装
- XListView
- tabbar文字字体颜色修改问题
- 洛谷 2346 四子连棋 bfs
- spring学习笔记二十 隔离级别、回滚、只读、过期
- JVM的内存
- java-pdf转word2.0
- jQuery与JavaScript 原生DOM API()用哪个好?
- Leetcode代码学习周记——Add Two Nmubers
- 51Nod-1174 区间中最大的数(RMQ)
- spring 整合 redis,以及spring的RedisTemplate如何使用
- Java集合框架要点概括(Core Knowledge of Java Collection)