随笔分类 -  POJ(图的搜索)

摘要:http://poj.org/problem?id=2049题意:有一个迷宫,迷宫中有墙、门和空地。有M道墙,每一道墙用(x,y,d,t)表示,(x,y)表示墙的起始坐标,(d=1,t)表示向上t个单位都是墙;(d=0,t)表示向右t个单位都是墙。有N扇门,用(x,y,d)表示,(x,y)表示门的起始坐标,d=1,表示向上一个单位都是门;d=0,表示向右一个单位都是门。给出Nemo的起始位置(f1,f2),问起点到(0,0)的最少要穿过的门。表示对搜索的题很晕。。看到题不知道该怎么存,看了别人的题解才懂点。。 1 #include 2 #include 3 #include 4 ... 阅读全文
posted @ 2014-02-24 20:59 N_ll 阅读(270) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1416题意:将一个数分成几部分,使其分割的各个数的和最大并且小于所给的数。凌乱了。。参考的会神的代码。。orz... 1 #include 2 #include 3 4 int arr[102],ans[102]; 5 int max,len,f; 6 int a,b; 7 8 void dfs(int n,int now,int sum,int k,int t) 9 {10 int m = n % 10;11 if (!n)12 {13 arr[k] = now;14 if ... 阅读全文
posted @ 2013-08-30 21:53 N_ll 阅读(206) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2676填九宫格思路:将每一行,每一列及每一个3*3块中出现的数字标记上,将可填的空的位置记录下来,枚举1-9,填入合适的数。 1 #include 2 #include 3 int row[10][10],col[10][10],arr[10][10];//row[x][i]表示i所在的行为x; 4 //col[y][i]表示i所在的列为y; 5 //arr[n][i]表示i所在... 阅读全文
posted @ 2013-08-30 17:26 N_ll 阅读(178) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2531不太理解这个代码。。。 1 #include 2 #include 3 int map[32][32],v[32]; 4 int n,max; 5 void dfs(int row,int sum) 6 { 7 8 int ans = sum; 9 v[row] = 1;10 for (int i = 1; i max)18 max = ans;19 for (int i = row+1; i sum)22 {23 dfs(i,ans);... 阅读全文
posted @ 2013-08-30 11:39 N_ll 阅读(206) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1321思路:按行搜索,回溯时还原棋盘。 1 #include 2 #include 3 int map[9][9],vis[9]; 4 int ans,n,k; 5 void dfs(int row,int cnt) 6 { 7 8 if (k==cnt) 9 {10 ans++;11 return ;12 }13 if (row > n)14 return ;15 for (int i = 1; i <= n; i ++)16 {17 ... 阅读全文
posted @ 2013-08-30 10:29 N_ll 阅读(145) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3009题意:一个小球要从2走到3,1代表障碍物,0代表可走。要求如果小球没遇到障碍物1,则小球按原来的方向一直走,直到遇到障碍物,障碍物变成0,小球任选一个方向继续走,此时步数加1,问从2走到3最少需要几步。 1 #include 2 #include 3 int map[32][32]; 4 int s_x,s_y,e_x,e_y,min; 5 int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}}; 6 void dfs(int x,int y,int step,int d) 7 { 8 if... 阅读全文
posted @ 2013-08-30 09:21 N_ll 阅读(218) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2488题意:给定一个row*col 的棋盘,列上用字母A,B,C······ 表示,行上用数字1,2,3······表示,问马是否能走遍整个棋盘,并将走的路径按字典序顺序输出。思路:只要按照dir[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};这个方向搜索,得出的路径即按字典序输出的。 1 #include 2 #include 3 ch 阅读全文
posted @ 2013-08-30 00:10 N_ll 阅读(155) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3414题意:给出1号杯子的容量a,2号杯子的容量b,目标盛水量c。问两个杯子经过怎样的变换能盛出c容量的水,输出最小步数及转换过程,如不能转换,输出“‘impossible”。思路:总共有6种转换,bfs搜索每一种转换,并记录前一个状态。最后将记录的状态倒着输出。 1 #include 2 #include 3 #include 4 #include 5 #include 6 const int N=112; 7 using namespace std; 8 string oper[7] = {"","FILL 阅读全文
posted @ 2013-08-29 00:43 N_ll 阅读(192) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3126题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后的数都是素数。思路:bfs+枚举每一位+素数筛选。 1 #include 2 #include 3 #include 4 using namespace std; 5 const int N=10000; 6 int prime[N],vis[N],b[4]; 7 void is_prime() 8 { 9 memset(prime,0,sizeof(prime));10 for (in... 阅读全文
posted @ 2013-08-28 11:17 N_ll 阅读(190) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2251题意:输出从S->E的最少时间,即最少步数。BFS搜索六个方向。 1 #include 2 #include 3 #include 4 using namespace std; 5 int e_x,e_y,e_z; 6 int n,row,col,vis[32][32][32]; 7 char map[32][32][32]; 8 int dir[6][3] = {{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0}}; 9 struct node10 {11 int x;... 阅读全文
posted @ 2013-08-27 21:56 N_ll 阅读(221) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3083S为起点,E为中点,输出S—>E沿左边走的步数,沿右边走的步数,和最短步数。因为S—>E沿左边走的步数等于E—>S沿右边走的步数,故只需DFS搜索沿右边走的步数,改变起止点即可。然后BFS搜索最少步数。 1 #include 2 #include 3 #include 4 #include 5 6 using namespace std; 7 const int N=55; 8 char map[N][N]; 9 int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};//顺时针方向1 阅读全文
posted @ 2013-08-14 11:26 N_ll 阅读(181) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3278 1 #include 2 #include 3 #include 4 #define MAX 1000002 5 using namespace std; 6 int vis[MAX],step[MAX]; 7 int dir[3][2] = {{1,1},{1,-1},{2,0}}; 8 queue q; 9 int n,m;10 void bfs(int s)11 {12 vis[s] = 1;13 q.push(s);14 while(!q.empty())15 {16 s =... 阅读全文
posted @ 2013-08-14 11:16 N_ll 阅读(223) 评论(0) 推荐(0)