搜索算法集锦 Ⅰ

来源:互联网 发布:qq账号解封软件 编辑:程序博客网 时间:2024/06/05 15:29

搜索:带有方向性的枚举(自我理解)
分类:广度优先搜索(BFS)和深度优先搜索(DFS)(含有回溯)

[hdu1312:] (http://acm.hdu.edu.cn/showproblem.php?pid=1312)
题目描述:
Red and Black

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13171 Accepted Submission(s): 8163

Problem Description
There is a rectangular(矩形) room, covered with square tiles. Each tile(地砖) is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent(临近的) tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)

Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial(最初的) tile (including itself).

Sample Input
6 9
….#.
…..#
……
……
……
……
……

@…

.#..#.
11 9
.#………
.#.#######.
.#.#…..#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#…….#.
.#########.
………..
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..

.

…@…

.

..#.#..
..#.#..
0 0

Sample Output
45
59
6
13

【言简意赅】:
计算从’@’开始能经过的所有’.’的数量,路径可重复,但总数不重复记录

广搜代码实现:

#include <iostream>#include <string.h>#include <stdio.h>using namespace std;const int maxn=30;char a[maxn][maxn];//存图数组bool b[maxn][maxn];//判重数组int qx[maxn*maxn],qy[maxn*maxn];int vb[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};//上下左右四个方向int ans,m,n;void bfs(int x,int y){    int l=0,r=0;    qx[r]=x;    qy[r++]=y;    //qx[r]=x;qy[r]=y;r++;    b[x][y]=1;    ans++;    while(l<r)    {        int cx=qx[l];        int cy=qy[l++];        //int cx=qx[l];int cy=qy[l];l++;        for(int i=0; i<4; i++)        {            int nx=cx+vb[i][0];            int ny=cy+vb[i][1];            if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&!b[nx][ny]&&a[nx][ny]!='#')            {                ans++;                b[nx][ny]=1;//状态标记                qx[r]=nx;qy[r++]=ny;//注意!此处是状态转移                //qx[r]=nx;qy[r]=ny;r++;            }        }    }}int main(){    int x,y;    while(scanf("%d%d",&m,&n)!=EOF)//此题是应用广搜    {        getchar();        if(m==0&&n==0)        break;        memset(a,0,sizeof(a));        memset(b,false,sizeof(b));        for(int i=1; i<=n; i++)        {            for(int j=1; j<=m; j++)            {                cin>>a[i][j];                if(a[i][j]=='@')//由于只有一个'@',即可以在输入时就将其下标记录下来                {                    x=i;                    y=j;                }            }        }        ans=0;        bfs(x,y);        cout<<ans<<endl;    }    return 0;}

深搜代码实现:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn=30;int n,m;int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};char map[maxn][maxn];bool vis[maxn][maxn];int ans;void dfs(int x,int y){    vis[x][y]++;    ans++;    for(int i=0; i<4; i++)//在可选的4个方向上依次实现搜索    {        int nx=x+dir[i][0];        int ny=y+dir[i][1];        if(nx<1||nx>n) continue;        if(ny<1||ny>m) continue;        if(vis[nx][ny]||map[nx][ny]=='#') continue;        dfs(nx,ny);//递归实现深度优先搜索    }}int main(){    int ca,a,b,cas=1;    cin>>ca;    while(ca--)    {        cin>>m>>n;        int sx,sy;        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)            {                cin>>map[i][j];                if(map[i][j]=='@') sx=i,sy=j;            }        memset(vis,0,sizeof(vis));        ans=0;        dfs(sx,sy);        cout<<"Case "<<cas++<<": "<<ans<<endl;    }    return 0;}
0 0
原创粉丝点击