博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Luogu P1649 [USACO07OCT]障碍路线Obstacle Course
阅读量:4542 次
发布时间:2019-06-08

本文共 3578 字,大约阅读时间需要 11 分钟。

题目描述

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 . .

Bessie finds herself in one such field at location A and wants to move to location B in order to lick the salt block there. Slow, lumbering creatures like cows do not like to turn and, of course, may only move parallel to the edges of the square field. For a given field, determine the minimum number of ninety degree turns in any path from A to B. The path may begin and end with Bessie facing in any direction. Bessie knows she can get to the salt lick.

N*N(1<=N<=100)方格中,’x’表示不能行走的格子,’.’表示可以行走的格子。卡门很胖,故而不好转弯。现在要从A点走到B点,请问最少要转90度弯几次?

输入输出格式

输入格式:

 

第一行一个整数N,下面N行,每行N个字符,只出现字符:’.’,’x’,’A’,’B’,表示上面所说的矩阵格子,每个字符后有一个空格。

【数据规模】

2<=N<=100

 

输出格式:

 

一个整数:最少转弯次数。如果不能到达,输出-1。

 

输入输出样例

输入样例#1:
3. x A. . .B x .
输出样例#1:
2

说明

【注释】

只可以上下左右四个方向行走,并且不能走出这些格子之外。开始和结束时的方向可以任意。

1//我用的DFS,他们说dfs做不出来,但我做出来了QWQ     #include
2 using namespace std; 3 int read() 4 { 5 int ret=0,ok=1;char ch=getchar(); 6 while(ch<'0'||ch>'9') {
if(ch=='-')ok=-1;ch=getchar();} 7 for(;ch>='0'&&ch<='9';ch=getchar()) ret=ret*10+ch-'0'; 8 return ret*ok; 9 } 10 int n; 11 char a[105][105]; 12 int ans[105][105]; 13 bool vis[105][105],sea[105][105]; 14 int bx[]={-1,1,0,0}; 15 int by[]={
0,0,-1,1}; 16 inline void dfs(int sx,int sy) 17 { 18 sea[sx][sy]=true; 19 if(sx>n || sy>n || sy<=0 || sx<=0) 20 return; 21 vis[sx][sy]=1; 22 for(int i=sx+1;i<=n;i++) 23 { 24 if(a[i][sy]=='x') 25 break; 26 if(ans[i][sy]>ans[sx][sy]+1) 27 { 28 ans[i][sy]=ans[sx][sy]+1; 29 dfs(i,sy); 30 } 31 vis[i][sy]=true; 32 } 33 for(int i=sx-1;i>=1;i--) 34 { 35 if(a[i][sy]=='x') 36 break; 37 if(ans[i][sy]>ans[sx][sy]+1) 38 { 39 ans[i][sy]=ans[sx][sy]+1; 40 dfs(i,sy); 41 } 42 vis[i][sy]=true; 43 } 44 for(int i=sy+1;i<=n;i++) 45 { 46 if(a[sx][i]=='x') 47 break; 48 if(ans[sx][i]>ans[sx][sy]+1) 49 { 50 ans[sx][i]=ans[sx][sy]+1; 51 dfs(sx,i); 52 } 53 vis[sx][i]=true; 54 } 55 for(int i=sy-1;i>=1;i--) 56 { 57 if(a[sx][i]=='x') 58 break; 59 if(ans[sx][i]>ans[sx][sy]+1) 60 { 61 ans[sx][i]=ans[sx][sy]+1; 62 dfs(sx,i); 63 } 64 vis[sx][i]=true; 65 } 66 for(int i=1;i<=4;i++) 67 { 68 int nx=sx+bx[i]; 69 int ny=sy+by[i]; 70 if(nx>n || ny>n || ny<=0 || nx<=0) 71 continue; 72 if(vis[nx][ny] && !sea[nx][ny]) 73 { 74 sea[nx][ny]=1; 75 dfs(nx,ny); 76 } 77 } 78 } 79 int main() 80 { 81 int sx,sy; 82 int lx,ly; 83 for(int i=1;i<=100;i++) 84 for(int j=1;j<=100;j++) 85 ans[i][j]=1234567; //这一步赋值很关键!之前用memset赋0x7f,会错一个点 86 n=read(); 87 for(int i=1;i<=n;i++) 88 for(int j=1;j<=n;j++) 89 { 90 cin>>a[i][j]; 91 if(a[i][j]=='A') 92 { 93 sx=i; 94 sy=j; 95 ans[i][j]=-1; 96 } 97 if(a[i][j]=='B') 98 { 99 lx=i;100 ly=j;101 }102 }103 dfs(sx,sy);104 if(ans[lx][ly]==1234567)105 {106 cout<<-1<

 

转载于:https://www.cnblogs.com/Hammer-cwz-77/p/7391499.html

你可能感兴趣的文章
阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第2节 线程实现方式_1_并发与并行...
查看>>
asp.net web.config配置节说明(转发)
查看>>
PPT幻灯片放映时无法全屏的解决方法
查看>>
开发中少不了的Fun -- 微信开发IOS端alert/confirm提示信息,去除网址(URL)的方法...
查看>>
Hibernate学习(二)
查看>>
java IO笔记(DataInput/DataOutput)
查看>>
Day8:String
查看>>
SQL语法之初级增删改查
查看>>
[转] Python基本学习资源收集汇总
查看>>
敏捷冲刺集合帖
查看>>
HTML5之Canvas
查看>>
团队作业8——第二次项目冲刺(Beta阶段)5.27
查看>>
彻底理解this指向
查看>>
hdu 3468(二分匹配)
查看>>
抑郁症的自我测试
查看>>
计算机编程以及基础原理
查看>>
关于多选框的问题
查看>>
使用.NET Reflector
查看>>
【剑指offer】40、最小的K个数
查看>>
JAVA生成RSA非对称型加密的公钥和私钥(利用JAVA API)
查看>>