BFS练习】的更多相关文章

图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则任选一个访问之:反之,退回到最近访问过的顶点:直到与起始顶点相通的全部顶点都访问完毕: 3.若此时图中尚有顶点未被访问,则再选其中一个顶点作为起始顶点并访问之,转 2: 反之,遍历结束. 连通图的深度优先遍历类似于树的先根遍历 如何判别V的邻接点是否被访问? 解决办法:为每个顶点设立一个“访问标志”…
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Submit][Status][Discuss] Description The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders…
传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25290 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 10…
传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11109 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be f…
这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而Helen达到(1,2),这种情况也算是相遇. #include<bits/stdc++.h> using namespace std; ][] = {{-, }, {, }, {, -}, {, }}; ][][][]; ]; int n,m; ][]; struct Node{ int x1,…
题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++.h> using namespace std; struct Node{ int num;//储存状态 vector<int> path;//储存操作 }; ]; int click(int i, int num){ ; switch(i){ :tmp = num ^ ; break;//点…
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h> using namespace std; bool isPrime(int n){//素数判断 || n == ) return true; else{ ; ; i < k; i++){ ) return false; } return true; } } ]; ]; void getPrime…
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以参考,其可用数学归纳法证明:http://www.cnblogs.com/1-2-3/archive/2011/04/25/generate-permutation-part2.html #include <bits/stdc++.h> using namespace std; map<in…
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int op_a(int n) {//操作A(上下行互换) ; ; + high; } int op_b(int n) {//操作B(每次以行循环右移一个) ; ; ; high = h * + high / ; ; low = l * + low / ; + low; } int op_c(int n) {//操作C…
//POJ3083 //DFS求靠左墙(右墙)走的路径长+BFS求最短路 //Time:0Ms Memory:716K #include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; #define MAX 45 #define INRANGE(x,y) (x >= 0 && x < n && y…