Poj 1708 Game (BFS)
来源:互联网 发布:linux beam.smp 编辑:程序博客网 时间:2024/06/01 18:29
这道题的描述真心纠结……
题意:给出N个点(不超过100个),每个点有一个颜色(颜色标号不超过100),有一些点的颜色可以相同。给出M条有向边,这些边也有一个颜色。给出两个棋子的初始点L,K,终点Q,问两个棋子中的任意一个是否可以移动到Q,如果可以,输出移动的最小步数。移动时遵循以下规则:
1.一个棋子可以移动到下一个点需要满足:经过的边的颜色与另一个棋子所在点的颜色相同;
2.只能沿着有向边的方向;
3.两个棋子不能同时移动到同一个点;
4.移动次序任意,没有必要交替进行;
5.其中一个棋子到达Q点时游戏结束。
思路:BFS,注意判断两个棋子不能同时到同一个点。一个点有多条出边,数组要开得大一些。
#include <cstdio>#include <iostream>#include <queue>using namespace std;const int NUM=105;const int INF=0x3fffffff;struct Point{int color; //该点的颜色int cnt; //该店的出度int to[1005]; //记录从该点出发的有向边int edgecolor[1005]; //记录有向边的颜色void Get (int v,int color){to[cnt]=v;edgecolor[cnt++]=color;}}point[NUM];int N,L,K,Q,ans;bool visit[NUM][NUM];struct Step //记录每一步{int first,second,cnt; //第一个棋子,第二个棋子,步数Step (int _f,int _s,int _c){first=_f;second=_s;cnt=_c;}~Step (){}};queue <Step> q;bool Bfs (){Step temp(L,K,0);visit[L][K]=true;q.push (temp);bool flag=false; //标记是否找到while (!q.empty()){temp=q.front();q.pop();if (temp.first == Q || temp.second == Q){flag=true;if (temp.cnt<ans)ans=temp.cnt;break;}int i;Point curf=point[temp.first];Point curs=point[temp.second];for (i=0;i<curf.cnt;i++)if (visit[curf.to[i]][temp.second]==false && curf.to[i] != temp.second && curf.edgecolor[i] == curs.color){visit[curf.to[i]][temp.second] = true;Step temp2(curf.to[i],temp.second,temp.cnt+1);q.push (temp2);}for (i=0;i<curs.cnt;i++)if (visit[temp.first][curs.to[i]]==false && curs.to[i] != temp.first && curs.edgecolor[i] == curf.color){visit[temp.first][curs.to[i]] = true;Step temp2(temp.first,curs.to[i],temp.cnt+1);q.push(temp2);}}return flag;}void Init () //初始化{ans=INF;memset (point,0,sizeof(point));memset (visit,false,sizeof(visit));while (!q.empty())q.pop();}int main (){while (~scanf("%d%d%d%d",&N,&L,&K,&Q)){int i,T;Init ();for (i=1;i<=N;i++)scanf("%d",&point[i].color);scanf("%d",&T);int u,v,color;for (i=1;i<=T;i++){scanf("%d%d%d",&u,&v,&color);point[u].Get(v,color);}if (Bfs())printf("YES\n%d\n",ans);elseprintf("NO\n");} return 0;}
- Poj 1708 Game (BFS)
- poj 1101 The Game BFS
- poj 1101 The Game(bfs)
- POJ 1753 Flip Game BFS
- poj 1101 The Game bfs
- POJ 1753 Flip Game(BFS)
- POJ 1753 Flip Game (BFS)
- [POJ 1101]The Game Bfs
- POJ 1753 Flip Game BFS/DFS
- POJ 1101 The Game (bfs)
- POJ 1753 Flip Game ------- BFS & 位运算
- 位运算-BFS-Flip Game-POJ 1753
- POJ 1027 The Same Game BFS
- 【BFS+状态压缩】-POJ-1753-Flip Game
- POJ 1753 Flip Game(bfs)
- poj 1753 Flip Game(位运算+bfs)
- POJ 1753 Flip Game(状态压缩BFS)
- POJ 1753 Flip Game(BFS+状态压缩)
- Uva 151 - Power Crisis
- 开源资料:firework 脚本方面的资料
- 第九天 CSS表单设计
- 开源软件下载地址
- Apple Sample代码摘抄-plist文件加载
- Poj 1708 Game (BFS)
- 该怎么样去选方向?(软件开发还是软件测试?)
- ZOJ 1109 Language of FatMouse
- 《学习OpenCV》练习题第四章第三题a
- webkit真正可以编译的过程
- 给ActiveX控件授权,加数字签名
- 开源项目之Android RibbonMenu(导航菜单)
- stl中的heap使用
- Joomla 3.x_component如何打包安装和卸载