poj3170 Knights of Ni BFS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yalishiyanzhouyu888/article/details/52292438

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 she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible.

Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000).

The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery.

In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block.

It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.

Input
Line 1: Two space-separated integers: W and H.

Lines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map’s description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row.

The integers that describe the map come from this set:
0: Square through which Bessie can travel
1: Impassable square that Bessie cannot traverse
2: Bessie’s starting location
3: Location of the Knights of Ni
4: Location of a shrubbery

Output
Line 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.

Sample Input
8 4
4 1 0 0 0 0 1 0
0 0 0 1 0 1 0 0
0 2 1 1 3 0 4 0
0 0 0 4 1 1 1 0

Sample Output
11

Hint
Explanation of the sample:

Width=8, height=4. Bessie starts on the third row, only a few squares away from the Knights.

Bessie can move in this pattern to get a shrubbery for the Knights: N, W, N, S, E, E, N, E, E, S, S. She gets the shrubbery in the northwest corner and then makes her away around the barriers to the east and then south to the Knights.

题目大意
给你一个n*m的矩阵,你需要从2到4再到3,求最少步数

输入
第一行两个整数m,n(注意:先输入m,再输入n
接下来一个n*m的矩阵

输出
一个整数,代表最少步数

这道题我一开始把一个<=写成了<,然后一直wa

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<set>
#include<queue>
using namespace std;
const int maxn=1001,inf=1e9;
int a[maxn][maxn],d[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 
int e,st[maxn][maxn],ed[maxn][maxn],n,m;
int sx,sy,tx,ty;
struct node{
    int x,y,tmp;
};
int mine=inf;
void bfs1(){
    int i,j;
    queue <node> q;
    node u;
    u.x=sx;
    u.y=sy;
    u.tmp=0;
    q.push(u);
    st[sx][sy]=0;
    while(!q.empty()){
        node to;
        to=q.front();
        q.pop();
        if(to.x==tx && to.y==ty) break;
        for(i=0;i<4;i++){
            node v;
            v.x=to.x+d[i][0];
            v.y=to.y+d[i][1];
            if(a[v.x][v.y]!=1 && a[v.x][v.y]!=3 && to.tmp+1<st[v.x][v.y] && v.x>0 && v.x<=n && v.y<=m && v.y>0){
                v.tmp=to.tmp+1;
                q.push(v);
                st[v.x][v.y]=to.tmp+1;
            }
        }
    }
}
void bfs2(){
    int i,j;
    queue <node> q;
    node u;
    u.x=sx;
    u.y=sy;
    u.tmp=0;
    q.push(u);
    ed[sx][sy]=0;
    while(!q.empty()){
        node to;
        to=q.front();
        q.pop();
        for(i=0;i<4;i++){
            node v;
            v.x=to.x+d[i][0];
            v.y=to.y+d[i][1];
            if(a[v.x][v.y]!=1 && to.tmp+1<ed[v.x][v.y] && v.x>0 && v.x<=n && v.y<=m && v.y>0){
                v.tmp=to.tmp+1;
                q.push(v);
                ed[v.x][v.y]=to.tmp+1;
            }
        }
    }
}
int main(){
    int i,j,k;
    int x1,y1,x2,y2;
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);
    scanf("%d%d",&n,&m);
    swap(n,m);
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
            if(a[i][j]==2){
                x1=i;
                y1=j;
            }
            if(a[i][j]==3){
                x2=i;
                y2=j;
            }
    }
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++){
            st[i][j]=inf;
            ed[i][j]=inf;
        }
    sx=x1;
    sy=y1;
    bfs1();
    sx=x2;
    sy=y2;
    bfs2();
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
            if(a[i][j]==4)
                mine=min(mine,st[i][j]+ed[i][j]);
    printf("%d\n",mine);
    return 0;
}

Knights of Ni

10-04

DescriptionnnBessie 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 she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible. nnBessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000). nnThe map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery. nnIn order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block. nnIt is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.nInputnnLine 1: Two space-separated integers: W and H. nnLines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row. nnThe integers that describe the map come from this set: n0: Square through which Bessie can travel n1: Impassable square that Bessie cannot traverse n2: Bessie's starting location n3: Location of the Knights of Ni n4: Location of a shrubberynOutputnnLine 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.nSample Inputnn8 4n4 1 0 0 0 0 1 0n0 0 0 1 0 1 0 0n0 2 1 1 3 0 4 0n0 0 0 4 1 1 1 0nSample Outputnn11

Knights

08-02

Problem DescriptionnAt the start of this game, there are N knights on a road of length N+1. The knights are numbered from 1 to N, and the ith knight stands i unit from the left end of the road.nnWhen the game begins, each knight moves to left or right at the same speed. Whenever a knight reaches to the end of the road, he instantly changes his direction. nnWhenever two knights meet, they fight until one loses, where each one of them wins in 50% possibility. Then the winner keeps moving in the direction he was moving before the fight, and the loser quits the game. The fighting time is very short and can be ignored.nnThe game continues until only one knight remains, and that knight is the winner.nnNow, we know the moving direction of each knight initially. Can you calculate the possibility that Nth knight win the game?n nnInputnThe first line of the input gives the number of test cases T (T <= 10). In each test case, the first line is an integer N (1 <= N <= 1000). The second line contains N integers. The ith integer represents the ith knight’s moving direction, and 0 stands for left while 1 stands for right.n nnOutputnEach test case contains one line and one integer. Let’s assume the possibility be equal to the irreducible fraction P / Q. Print the value of P⋅Q−1 in the prime field of integers modulo 1 000 000 007(109+7). It is guaranteed that this modulo does not divide Q, thus the number to be printed is well-defined.n nnSample Inputn2n2n0 0n3n0 1 0n nnSample OutputnCase #1: 500000004nCase #2: 250000002

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭