跳过正文
  1. Posts/

poj 3984 迷宫问题

·1 分钟

迷宫问题

 1
 2
 3
 4    /*************************************************************************
 5    	> File Name: code/2015summer/searching/KK.cpp
 6    	> Author: 111qqz
 7    	> Email: rkz2013@126.com
 8    	> Created Time: 2015年07月25日 星期六 13时33分00秒
 9     ************************************************************************/
10
11    #include<iostream>
12    #include<iomanip>
13    #include<cstdio>
14    #include<algorithm>
15    #include<cmath>
16    #include<cstring>
17    #include<string>
18    #include<map>
19    #include<set>
20    #include<queue>
21    #include<vector>
22    #include<stack>
23    #define y0 abc111qqz
24    #define y1 hust111qqz
25    #define yn hez111qqz
26    #define j1 cute111qqz
27    #define tm crazy111qqz
28    #define lr dying111qqz
29    using namespace std;
30    #define REP(i, n) for (int i=0;i<int(n);++i)
31    typedef long long LL;
32    typedef unsigned long long ULL;
33    int a[10][10];
34    int head = 0;
35    int tail = 1;
36    int dirx[2]={1,0};
37    int diry[2]={0,1};
38    struct node
39    {
40        int x,y,pre;
41    }q[10];
42
43    void print(int x)
44    {
45        if (q[x].pre!=-1)
46        {
47    	  print(q[x].pre);
48    	  printf("(%d, %d)\n",q[x].x,q[x].y);
49        }
50    }
51    void bfs()
52    {
53        q[head].x=0;
54        q[head].y=0;
55        q[head].pre=-1;
56        while (head<tail)
57        {
58    	  if (q[head].x==4&&q[head].y==4)
59    	  {
60    		print(head);
61    		return;
62    	  }
63    	  for (int i = 0 ; i < 2 ; i++ )
64    	  {
65    		int newx=dirx[i]+q[head].x;
66    		int newy=diry[i]+q[head].y;
67    		if (newx>=0&&newx<5&&newy>=0&&newy<5&&a[newx][newy]==0)
68    		{
69    		    q[tail].x=newx;
70    		    q[tail].y=newy;
71    		    q[tail].pre=head;
72    		    tail++;
73    		}
74    	  }
75    	  head++;
76        }
77
78    }
79    int main()
80    {
81        for ( int i = 0 ; i < 5 ; i++ )
82        {
83    	  for ( int j = 0 ; j < 5;  j++)
84    	  {
85    		cin>>a[i][j];
86    	  }
87        }
88        printf("(0, 0)\n");
89        bfs();
90
91    	return 0;
92    }

相关文章

poj 3087 Shuffle'm Up (bfs)

·1 分钟
http://poj.org/problem?id=3087 用bfs写的,但是其实就是个模拟啊喂! 只有一种操作,何谈最短? 一直往下写就行了.

poj 3126 Prime Path (bfs)

·2 分钟
http://poj.org/problem?id=3126 题意是说,给定两个四位素数a b 问从a变换到b,最少需要变换几次. 变换的要求是,每次只能改变一个数字,而且中间过程得到的四位数也必须为素数. 因为提到最少变换几次,容易想到bfs,bfs第一次搜到的一定是最短步数.

poj 3278 catch that cow

·1 分钟
http://poj.org/problem?id=3278 bfs,用到了stl的queue 1 2 3 /* *********************************************** 4 Author :111qqz 5 Created Time :2016年02月19日 星期五 15时45分05秒 6 File Name :3278.cpp 7 ************************************************ */ 8 9 #include <algorithm> 10 #include <cstdio> 11 #include <iostream> 12 #include <cstring> 13 #include <string> 14 #include <cmath> 15 #include <map> 16 #include <stack> 17 #include <queue> 18 19 using namespace std; 20 typedef long long LL; 21 const int inf = 8E8; 22 const int N=2E5+7; 23 int d[N]; 24 int n,k; 25 void bfs() 26 { 27 queue<int> q; 28 memset(d,-1,sizeof(d)); 29 q.push(n); 30 d[n]=0; 31 while (!q.empty()) 32 { 33 int x = q.front(); 34 q.pop(); 35 if ( x==k ) 36 { 37 break; 38 } 39 int next[10]; 40 next[1]=x-1; 41 next[2]=x+1; 42 next[3]=2*x; 43 for ( int i = 1; i <= 3 ; i++ ) 44 { 45 if (next[i]>=0&&next[i]<=100000&&d[next[i]]==-1) 46 { 47 d[next[i]]=d[x]+1; 48 q.push(next[i]); 49 } 50 } 51 } 52 53 54 } 55 int main() 56 { 57 while (scanf("%d %d",&n,&k)!=EOF) 58 { 59 bfs(); 60 cout<<d[k]<<endl; 61 } 62 return 0; 63 }

poj 3279 Fliptile (搜索..暴力?)

·2 分钟
http://poj.org/problem?id=3279 反转类问题. 有N*M个方格,每个上面有数字0或者1 操作一个方格,这个方格即其相邻的四个方格(有公共边)会改变状态(由0变1或者由1变0)