HDU1312——Red and Black

http://acm.hdu.edu.cn/showproblem.php?pid=1312

这倒题用的是广搜:广搜用的是队列。就是从一个点有四个方向,所以把四个可能都放进队列里。

广搜也是状态改变。就像是泼水的感觉。

题意:就是从@位置最多能走多少步。明显的广搜

#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
struct point{
    int x;
    int y; 
}; 
char G[25][25];
int  n, m,s,t;
int vis[25][25];
int ans;//ans用于记录步骤数 

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void bfs(){
    queue<point> que;
    point start;
    start.x = s, start.y = t;
    vis[s][t] = 1;
    que.push(start);
    while(!que.empty()){
        point u=que.front();//取队首 
        que.pop();//去队首 
        for(int i=0;i<4;i++){
            int x=u.x+dx[i];
            int y=u.y+dy[i];
            if(x < 0 || x >= m) continue;
            if(y < 0 || y >= n) continue;
            if(G[x][y]=='#'||vis[x][y])continue;//去过碰到障碍或者是这个点访问过就返回 
            vis[x][y]=1;//上述条件都不符合就标记访问过该点
            ans++;
            point next;
            next.x=x;
            next.y=y;
            que.push(next); 
        }
    } 
    
}
int main(){
     while(~scanf("%d%d", &n, &m) ) {
         if(m==0&&n==0)break;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        scanf("%s",G[i]);//字符串输入 
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(G[i][j]=='@'){
                s=i;
                t=j;
            }
        }
    }
    ans=1;  
    bfs();
    memset(vis, 0, sizeof(vis) );//数组赋初值 
    printf("%d
",ans);
}
    return 0;
} 
原文地址:https://www.cnblogs.com/Yvettey-me/p/4523593.html