随笔分类 -  搜索—BFS && DFS

摘要:做了1天,总是各种错误,很无语最后还是参考大神的方法题目:http://poj.org/problem?id=3083题意:从s到e找分别按照左侧优先和右侧优先的最短路径,和实际的最短路径DFS找左右侧 的最短路径 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 using namespace std; 10 char G[50][50]; 11 int vis[50][50]; 12 int c,r; 13 struct node 14 { ... 阅读全文
posted @ 2013-08-06 20:10 水门 阅读(180) 评论(0) 推荐(0)
摘要:题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2141&cid=1186 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 int map[101][110],vis[101]; 8 int n,m,k; 9 queueq;10 void bfs(int t)11 {12 int i,x,a[110],j;13 q.push(t);14 vis[t]=1; j=0;15 while(!q.empty()... 阅读全文
posted @ 2013-06-29 23:27 水门 阅读(222) 评论(0) 推荐(0)
摘要:题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2107&cid=1186 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 int map[101][110],vis[101],a[110]; 8 int n,m,k,j; 9 void dfs(int x)10 {11 int i;12 for(i=0; i>k>>m;31 for(i=0; i>u>>v;34 map[... 阅读全文
posted @ 2013-06-29 23:25 水门 阅读(467) 评论(0) 推荐(0)
摘要:题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 int vis[101]; 8 int n,m,k; 9 queueq;10 struct node11 {12 int u,v;13 struct node *next;14 }*head[110];15 void add(int u, int v)16 {17 struct no... 阅读全文
posted @ 2013-06-29 23:21 水门 阅读(282) 评论(0) 推荐(0)
摘要:题目:http://poj.org/problem?id=3278题意:给定两个整数n和k通过 n+1或n-1 或n*2 这3种操作,使得n==k输出最少的操作次数 1 #include 2 #include 3 #include 4 using namespace std; 5 int vis[200001]; 6 struct node 7 { 8 int x,step; 9 };10 int bfs(int n,int k)11 {12 if(n==k)13 return 0;14 queueq;15 struct node next,pos;16 ... 阅读全文
posted @ 2013-06-29 23:18 水门 阅读(197) 评论(0) 推荐(0)
摘要:题目:http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2449&cid=1181目前dfs 里的递归还是不很懂,AC代码如下: 1 #include 2 #include 3 int map[10][10],vis[10][10]; 4 int ans; 5 int m,n; 6 void dfs(int i,int j) 7 { 8 if(vis[i][j]||map[i][j]) return; 9 10 if(i==n&&j==m)11 {12 ans++;13 return;14 ... 阅读全文
posted @ 2013-06-24 22:59 水门 阅读(358) 评论(0) 推荐(0)
摘要://题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因此考虑用位表示。可以改变任意一个把手的位置,但同时改变其所在的行和列。求最小步骤.//耗时 800MS1 #include 2 #include 3 #include 4 #include 5 6 using namespace std; 7 int id,vis[1q;28 int i;29 struct node cur,next;30 next.step=0; next.... 阅读全文
posted @ 2013-06-21 21:24 水门 阅读(231) 评论(0) 推荐(0)
摘要:题目:http://poj.org/problem?id=1753因为粗心错了好多次……,尤其是把1#include#includeusing namespace std;int id,vis[1q; int i; struct node pos,cur; if(id==0||id==65535) return 0; memset(vis,0,sizeof(vis)); pos.step=0; pos.x=id; q.push(pos); vis[id]=1; while(!q.empty()) { cur=q.f... 阅读全文
posted @ 2013-06-20 22:38 水门 阅读(332) 评论(0) 推荐(0)
摘要:1 #include 2 #include 3 #include 4 char map[110][110],cpy[110][110]; 5 int vis[110][110]; 6 7 int dx[4]={0,0,1,-1}; 8 int dy[4]={1,-1,0,0}; 9 10 int tdx[8]={0,0,1,-1,1,-1,1,-1};11 int tdy[8]={1,-1,0,0,1,1,-1,-1};12 int n,i,j,cnt1,cnt2;13 14 void dfs(int i,int j)15 {16 int k;17 for(k=0; k=... 阅读全文
posted @ 2013-05-23 08:53 水门 阅读(120) 评论(0) 推荐(0)
摘要:也是以前做的一个题,当时不会。bfs: 1 #include<stdio.h> 2 #include<string.h> 3 int a,b,c; 4 int vis[55][55][55],map[55][55][55]; 5 6 int dx[6]={0,0,0,0,-1,1}; 7 int dy[6]={0,0,1,-1,0,0}; 8 int dz[6]={1,-1,0,0,0,0}; 9 struct node10 {11 int x,y,z;12 int time1;13 }pos,npos,queue[55*55*55];14 15 int bfs()16 阅读全文
posted @ 2013-05-23 08:41 水门 阅读(138) 评论(0) 推荐(0)