The Grove
Description
The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She's just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn't think you could pass 'through' the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take.
Happily, Bessie lives on a simple world where the pasture is represented by a grid with R rows and C columns (1 <= R <= 50, 1 <= C <= 50). Here's a typical example where '.' is pasture (which Bessie may traverse), 'X' is the grove of trees, '*' represents Bessie's start and end position, and '+' marks one shortest path she can walk to circumnavigate the grove (i.e., the answer): ...+... ..+X+.. .+XXX+. ..+XXX+ ..+X..+ ...+++*The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she's starting 'outside' the grove instead of in a sort of 'harbor' that could complicate finding the best path. Input
Line 1: Two space-separated integers: R and C
Lines 2..R+1: Line i+1 describes row i with C characters (with no spaces between them). Output
Line 1: The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.
Sample Input 6 7 ....... ...X... ..XXX.. ...XXX. ...X... ......* Sample Output 13 Source |
思路:
突破口肯定是必须要围绕果园走一圈了,那么起点到一个点分顺时针和逆时针经过果园用bfs求两次最短路就够了,比赛时想了很久都没想到处理顺时针和逆时针的方法,就只能想到这步了,思维还是不能突破呀。
怎样处理顺时针和逆时针呢?从果园中引一条射线出去与地图边界相交,从起点出发两次bfs,一次控制只能向上经过射线,一次控制只能向下进过射线就够了。因为绕果园必须要经过射线上一点,所以最后用射线上的点来更新答案就OK了。
注意:
那个射线不能随便作的,要保证射线不与果园再次相交。想一想,为什么?
ps:
本题原来写的实射线法有bug,当射线要过起始点的时候应该要换射线,不然会错,poj没有这种数据,所以开始就水过了,现在已改好了,有问题欢迎讨论。下面补充了虚拟射线的做法,下面的方法更加推荐。
实射线法:
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,m,ans;
int sx,sy;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,-1,1};
int dist[maxn][maxn][2];
bool vis[maxn][maxn][2];
char mp[maxn][maxn];
char s[maxn];
struct Node
{
int x,y;
} cur,now;
queue<Node>q;
void presolve()
{
int i,j,k;
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
if(mp[i][j]=='X')
{
if(sx==i&&sy<j) // 左射线经过* 换右射线
{
for(k=j;mp[i][k]=='X';k++) ;
for(;k<=m;k++)
{
mp[i][k]='Y';
}
}
else
{
for(k=j-1; k>=1; k--) // 将射线标记
{
mp[i][k]='Y';
}
}
return ;
}
}
}
}
void bfs(int k)
{
int i,j,nx,ny,tx,ty;
while(!q.empty()) q.pop();
cur.x=sx;
cur.y=sy;
dist[sx][sy][k]=0;
vis[sx][sy][k]=1;
q.push(cur);
while(!q.empty())
{
now=q.front();
q.pop();
nx=now.x;
ny=now.y;
for(i=0; i<8; i++)
{
tx=nx+dx[i];
ty=ny+dy[i];
if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='X'||vis[tx][ty][k]) continue ;
if(k&&mp[tx][ty]=='Y') // 到Y时控制过去的方向就好了
{
if(i==1||i==6||i==7) continue ;
}
else if(!k&&mp[tx][ty]=='Y') // 到Y时控制过去的方向就好了
{
if(i==0||i==4||i==5) continue ;
}
cur.x=tx;
cur.y=ty;
dist[tx][ty][k]=dist[nx][ny][k]+1;
vis[tx][ty][k]=1;
q.push(cur);
}
}
}
void solve()
{
int i,j;
ans=INF;
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
if(mp[i][j]=='Y')
{
ans=min(ans,dist[i][j][0]+dist[i][j][1]);
}
}
}
}
int main()
{
int i,j,t;
while(~scanf("%d%d",&n,&m))
{
for(i=1; i<=n; i++)
{
scanf("%s",s);
for(j=1; j<=m; j++)
{
mp[i][j]=s[j-1];
if(mp[i][j]=='*') sx=i,sy=j;
}
}
presolve();
memset(vis,0,sizeof(vis));
bfs(0);
bfs(1);
solve();
printf("%d\n",ans);
}
return 0;
}
/*
4 5
.....
..XX*
.....
.....
*/
虚拟射线法:
虚拟出一条射线,然后搜索图中看其是否穿过,穿过奇数次为在多边形内,偶数次为多边形外,自己可以画图看看效果,另外判断是否穿过的函数容易写错,因为状态很容易覆盖
ps:这种做法推荐,是一般解法,适合各种变形,而且代码简单。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 105
#define mod 1000000007
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,m,ans,flag;
int sx,sy,gx,gy,nx,ny,tx,ty;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,-1,1};
int dp[maxn][maxn][2];
char mp[maxn][maxn];
char s[maxn];
struct Node
{
int x,y,k; // k-是否包围
} cur,now;
bool issur() // 向下穿过包围 向上解包围
{
if(tx==gx&ty<gy)
{
if(nx<gx) return true;
}
else if(nx==gx&&ny<gy)
{
if(tx<gx) return true ;
}
return false;
}
void bfs()
{
int i,j,t,k,nk;
queue<Node>q;
memset(dp,-1,sizeof(dp));
cur.x=sx;
cur.y=sy;
cur.k=0;
dp[sx][sy][0]=0;
q.push(cur);
while(!q.empty())
{
now=q.front();
q.pop();
nx=now.x;
ny=now.y;
nk=now.k;
for(i=0; i<8; i++)
{
tx=nx+dx[i];
ty=ny+dy[i];
k=nk;
if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='X') continue ;
if(issur()) k^=1;
if(dp[tx][ty][k]!=-1&&dp[tx][ty][k]<=dp[nx][ny][nk]+1) continue ;
cur.x=tx;
cur.y=ty;
cur.k=k;
dp[tx][ty][k]=dp[nx][ny][nk]+1;
q.push(cur);
}
}
ans=dp[sx][sy][1];
}
int main()
{
int i,j,t;
while(~scanf("%d%d",&n,&m))
{
flag=0;
for(i=1; i<=n; i++)
{
scanf("%s",s);
for(j=1; j<=m; j++)
{
mp[i][j]=s[j-1];
if(mp[i][j]=='*') sx=i,sy=j;
if(!flag&&mp[i][j]=='X') flag=1,gx=i,gy=j;
}
}
bfs();
printf("%d\n",ans);
}
return 0;
}
/*
3 4
....
*.X.
....
6 7
.......
...X...
..XXX..
...XXX.
...X...
......*
*/