POJ 3170(Knights of Ni)(BFS)

题目地址:http://poj.org/problem?id=3170

思路:两次BFS,先从起点到shrubbery点求最短路,再从终点到shrubbery求最短路,枚举shrubbery点,取最小值。

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define debu
using namespace std;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
const int  INF=0x3f3f3f3f;
const int maxn=1000+50;
struct Node
{
    int x,y;
    Node(int x=0,int y=0):x(x),y(y) {}
};
queue<Node> q;
vector<Node> shr;
int n,m,stx,sty,edx,edy;
int dist[2][maxn][maxn];
int g[maxn][maxn],v[maxn][maxn];
void solve(int id,int x,int y)
{
    memset(v,0,sizeof(v));
    while(!q.empty()) q.pop();
    memset(dist[id],INF,sizeof(dist[id]));
    v[x][y]=1,q.push(Node(x,y)),dist[id][x][y]=0;
    while(!q.empty())
    {
        Node now=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            int x=now.x+dx[i];
            int y=now.y+dy[i];
            if(x>=1&&x<=n&&y>=1&&y<=m&&(g[x][y]!=1)&&(!v[x][y]))
            {
                dist[id][x][y]=dist[id][now.x][now.y]+1;
                v[x][y]=1,q.push(Node(x,y));
            }
        }
    }
}
int main()
{
#ifdef debug
    freopen("in.txt","r",stdin);
#endif // debug
    scanf("%d%d",&m,&n);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++)
        {
            scanf("%d",&g[i][j]);
            if(g[i][j]==4) shr.push_back(Node(i,j));
            if(g[i][j]==2) stx=i,sty=j;
            if(g[i][j]==3) edx=i,edy=j;
        }
    solve(0,stx,sty);
    solve(1,edx,edy);
    int ans=INF;
    for(int i=0; i<shr.size(); i++)
    {
        int x=shr[i].x;
        int y=shr[i].y;
        ans=min(ans,dist[0][x][y]+dist[1][x][y]);
    }
    printf("%d\n",ans);
    return 0;
}
时间: 2024-11-06 06:59:07

POJ 3170(Knights of Ni)(BFS)的相关文章

POJ 3278 Catch That Cow (有思路有细节的bfs)

Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo

poj3984 迷宫问题(简单的输出路径的bfs)

题目链接 http://poj.org/problem?id=3984 中文题题意不解释了 反正就是简单的结构体套结构体存一下路径就行了 #include <iostream> #include <cstring> #include <deque> #include <queue> using namespace std; int map[6][6]; struct ss { int x , y; }; struct TnT { deque<ss>

Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)

[题目链接] http://codeforces.com/problemset/problem/788/C [题目大意] 给出一些浓度的饮料,要求调出n/1000浓度的饮料,问最少需要多少升饮料 [题解] 设浓度为a,现在要求出系数x1,x2,x3……,使得x1*a1+x2*a2+x3*a3+……=n*(x1+x2+x3+……) 得a1*(x1-n)+a2*(x2-n)+a3*(x3-n)+……=0 假设现在有x1-n和x2-n,设其数值为x和y,那么一定有(x)*y+(-y)*x=0, x+y

C. Ice Cave (CF #301 (Div. 2) 搜索bfs)

C. Ice Cave time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to

UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

UVA 1600 Patrol Robot Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n columns). The rows are labeled from 1 to m. The colu

bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】

bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通行的区域 2:贝茜现在所在的位置 3:骑士们的位置 4:长着贝茜需要的灌木的土地 */ #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace s

POJ 3170 Knights of Ni(两次BFS啊)

题目链接:http://poj.org/problem?id=3170 Description Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that sh

【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3824 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, an

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反