Luogu 2346/codevs 1004(IDDFS)
来源:互联网 发布:k最短路径算法 matlab 编辑:程序博客网 时间:2024/04/27 14:56
传送门
题解:
可以BFS+Hash判重,但是本蒟蒻还是把此题当做迭代加深的模板题来做。
所谓迭代加深就是指每次限定一个迭代深度,到达这一层就返回,如果找不到解再扩大限制重复搜索直到搜到解,否则直接返回答案。
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;char s[5][5];int sx[2],sy[2],cnt=0,ans;int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};inline void swap(char &x,char &y) {x^=y^=x^=y;}inline bool cck() {for (int i=1;i<=4;++i) {if (s[i][1]==s[i][2]&&s[i][2]==s[i][3]&&s[i][3]==s[i][4]) return true;if (s[1][i]==s[2][i]&&s[2][i]==s[3][i]&&s[3][i]==s[4][i]) return true;}if (s[1][1]==s[2][2]&&s[2][2]==s[3][3]&&s[3][3]==s[4][4]) return true;if (s[1][4]==s[2][3]&&s[2][3]==s[3][2]&&s[3][2]==s[4][1]) return true;return false;}inline bool in(int x,int y,char c) {return x>0&&x<5&&y>0&&y<5&&s[x][y]^c;}bool IDDFS(int x1,int y1,int x2,int y2,char cur,int step) {if (step==ans) return cck();for (int i=0;i<4;++i) {int ax=x1+dx[i],ay=y1+dy[i],bx=x2+dx[i],by=y2+dy[i];if (in(ax,ay,cur)) {swap(s[ax][ay],s[x1][y1]);if (IDDFS(ax,ay,x2,y2,cur=='B'?'W':'B',step+1)) return true;swap(s[ax][ay],s[x1][y1]);}if (in(bx,by,cur)) {swap(s[bx][by],s[x2][y2]);if (IDDFS(x1,y1,bx,by,cur=='B'?'W':'B',step+1)) return true;swap(s[bx][by],s[x2][y2]);}}return false;}int main() {//freopen("P2346.in","r",stdin);for (int i=1;i<=4;++i) {scanf("%s",s[i]+1);for (int j=1;j<=4;++j)if (s[i][j]=='O') sx[cnt]=i,sy[cnt]=j,++cnt;}for (ans=1;ans<=0x3f3f3f3f;++ans) {if (IDDFS(sx[0],sy[0],sx[1],sy[1],'B',0)) {printf("%d\n",ans);return 0;}if (IDDFS(sx[0],sy[0],sx[1],sy[1],'W',0)) {printf("%d\n",ans);return 0;}}}
阅读全文
0 0
- Luogu 2346/codevs 1004(IDDFS)
- CodeVS&Luogu 间谍网络
- [codevs 1288] 埃及分数 [IDdfs 迭代加深搜索 ]
- codevs 2218 补丁vs错误(状压搜索)(luogu P1527)
- codevs 1136 luogu P1312 Mayan 游戏
- uva12558(埃及分数--难)(IDDFS入门题)
- 埃及分数问题 迭代加深搜索(IDDFS)
- UVA12558->IDDFS
- Luogu 1280(dp)
- Luogu 1282(dp)
- Luogu 1387(dp)
- Luogu 2661(tarjan)
- Luogu 2330(kruskal)
- Luogu 1156(dp)
- Luogu 1186(SPFA)
- Luogu 1141(dp)
- Luogu 3378(堆)
- Luogu 3370(hash)
- CSS 元素垂直居中的 6种方法
- 第八周 【项目1
- jQuery基本选择器
- 线程的同步与锁定-synchronized初解
- 自做京东购物车
- Luogu 2346/codevs 1004(IDDFS)
- Tomcat + IDEA + MYSQL实现App与服务端交流
- 【git基础】大白话解释git和github
- 欢迎使用CSDN-markdown编辑器
- 第8周项目1 建立顺序串的算法库
- 逆转链表
- Java 基础练习(2)-随机字符串
- 第八周项目3--对称矩阵压缩存储的实现与应用(3)
- python enumerate的解释