bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course

P1649 [USACO07OCT]障碍路线Obstacle Course

bfs

直接上个bfs

注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同)。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 #define pi pair<int,int>
 7 #define mkp make_pair
 8 #define N 105
 9 const int d1[4]={0,1,0,-1};
10 const int d2[4]={1,0,-1,0};
11 char q[3]; bool dan[N][N];
12 int a[N][N],vis[N][N],n;
13 pi S,T; queue<pi> h;
14 int main(){
15     scanf("%d",&n);
16     for(int i=1;i<=n;++i){
17         for(int j=1;j<=n;++j){
18             scanf("%s",q);
19             if(q[0]==‘A‘) S=mkp(i,j);
20             if(q[0]==‘B‘) T=mkp(i,j);
21             if(q[0]==‘x‘) dan[i][j]=1;
22         }
23     }h.push(S);
24     while(!h.empty()){
25         pi u=h.front(); h.pop();
26         for(int i=0;i<4;++i){
27             int r1=u.first+d1[i],r2=u.second+d2[i];
28             while(!dan[r1][r2]){
29                 if(r1>n||r1<1||r2>n||r2<1) break;
30                 if(vis[r1][r2]){
31                     r1+=d1[i];r2+=d2[i];
32                     continue;
33                 }
34                 vis[r1][r2]=vis[u.first][u.second]+1;
35                 if(mkp(r1,r2)==T){
36                     printf("%d",max(0,vis[r1][r2]-1));
37                     return 0;
38                 }h.push(mkp(r1,r2));
39                 r1+=d1[i];r2+=d2[i];
40             }
41         }
42     }printf("-1");
43     return 0;
44 }

P1649

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 #define pi pair<int,int>
 7 #define mkp make_pair
 8 #define N 105
 9 const int d1[4]={0,1,0,-1};
10 const int d2[4]={1,0,-1,0};
11 char q[N]; bool dan[N][N];
12 int a[N][N],vis[N][N],n;
13 pi S,T; queue<pi> h;
14 int main(){
15     scanf("%d",&n);
16     for(int i=1;i<=n;++i){
17         scanf("%s",q);
18         for(int j=1;j<=n;++j){
19             if(q[j-1]==‘A‘) S=mkp(i,j);
20             if(q[j-1]==‘B‘) T=mkp(i,j);
21             if(q[j-1]==‘x‘) dan[i][j]=1;
22         }
23     }h.push(S);
24     while(!h.empty()){
25         pi u=h.front(); h.pop();
26         for(int i=0;i<4;++i){
27             int r1=u.first+d1[i],r2=u.second+d2[i];
28             while(!dan[r1][r2]){
29                 if(r1>n||r1<1||r2>n||r2<1) break;
30                 if(vis[r1][r2]){
31                     r1+=d1[i];r2+=d2[i];
32                     continue;
33                 }vis[r1][r2]=vis[u.first][u.second]+1;
34                 if(mkp(r1,r2)==T){
35                     printf("%d",max(0,vis[r1][r2]-1));
36                     return 0;
37                 }h.push(mkp(r1,r2));
38                 r1+=d1[i];r2+=d2[i];
39             }
40         }
41     }
42 }

bzoj1644

原文地址:https://www.cnblogs.com/kafuuchino/p/10031718.html

时间: 2024-10-15 19:35:32

bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course的相关文章

洛谷 P1649 [USACO07OCT]障碍路线Obstacle Course

P1649 [USACO07OCT]障碍路线Obstacle Course 题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these tiles are impassible by cows and are marked with an 'x' in this 5 by 5 field that is challenging to navigate: . . B x .

[USACO07OCT]障碍路线Obstacle Course

题目描述 Consider an N x N (1 <= N <= 100) square field composed of 1 by 1 tiles. Some of these tiles are impassible by cows and are marked with an 'x' in this 5 by 5 field that is challenging to navigate: . . B x . . x x A . . . . x . . x . . . . . x .

[USACO07OCT]障碍路线 &amp; yzoj P1130 拐弯 题解

题意 给出n* n 的图,A为起点,B为终点,* 为障碍,.可以行走,问最少需要拐90度的弯多少次,无法到达输出-1. 解析 思路:构造N * M * 4个点,即将原图的每个点分裂成4个点.其中点(i,j,k)表示在(i,j)时人的方向是k,然后对于两个点(i,j,k)和(i,j,kk),如果k和kk是两个旋转90度能转换的方向,就连一条边权为1的边,而对于(i,j,k)和(i+dx[ k],j+dy[k],k)连一条边权为0的边,表示从(i,j)在方向为k的情况下能向k方向走一步到达(i+dx

[洛谷1649]障碍路线&lt;BFS&gt;

题目链接:https://www.luogu.org/problem/show?pid=1649 历经千辛万苦,我总算是把这个水题AC了,现在心里总觉得一万只草泥马在奔腾: 这是一道很明显的BFS,然后我也明显的看出来了 但是,我就是WA了很久很久,在调试第一个晚上后,我发现读入是存在空格的,而不是数据问题 然后第二个问题就是,BFS找到的第一个终点不一定就是最优的答案(当然第二个问题是我重新打了一次猛然发现之前没注意的一点) 注意到这两点,其实这道题就没难度了 然后这道题的处理需要注意一个地方

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

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 383  Solved: 196[Submit][Status][Discuss] Description 考虑一个 N x N (1 <= N <= 100)的有1个个方格组成的正方形牧场.有些方格是奶牛们不能踏上的,它们被标记为了'x'.例如下图: . . B x .. x x A .. . . x .. x .

BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课

题目 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课 Time Limit: 5 Sec  Memory Limit: 64 MB Description 考虑一个 N x N (1 <= N <= 100)的有1个个方格组成的正方形牧场.有些方格是奶牛们不能踏上的,它们被标记为了'x'.例如下图: . . B x .. x x A .. . . x .. x . . .. . x . . 贝茜发现自己恰好在点A处,她想去B处的盐块舔盐.缓慢而且笨拙的动物

BZOJ 1644: [Usaco2007 Oct]Obstacle Course 障碍训练课( BFS )

BFS... 我连水题都不会写了QAQ ------------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<queue> #define rep( i , n ) for( int i = 0 ; i &

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

原题: 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 columns are labeled from 1 to n. A cell (i, j) denotes the cell in row i and column j in the