【bzoj 3299】 [USACO2011 Open]Corn Maze玉米迷宫(最短路)

就一个最短路,并且边长都是1,所以每个点只搜一次。

 1 /**************************************************************
 2     Problem: 3299
 3     User: MT_Chan
 4     Language: C++
 5     Result: Accepted
 6     Time:72 ms
 7     Memory:2420 kb
 8 ****************************************************************/
 9
10 #include<cstdio>
11 #include<cstdlib>
12 #include<cstring>
13 #include<iostream>
14 #include<algorithm>
15 #include<queue>
16 using namespace std;
17 #define Maxn 310
18 #define INF 0xfffffff
19
20 int kx[30];
21 int a[Maxn][Maxn],tr[Maxn*Maxn];
22 char s[Maxn];
23
24 int dis[Maxn*Maxn],st,ed;
25 int n,m;
26
27 queue<int > q;
28 void spfa()
29 {
30     while(!q.empty()) q.pop();
31     memset(dis,-1,sizeof(dis));
32     q.push(st);dis[st]=0;
33     while(!q.empty())
34     {
35         int x=q.front();
36         int nx=(x-1)/m+1,ny=x-(nx-1)*m;
37         if(ny>1&&tr[x-1]!=-1&&dis[tr[x-1]]==-1)
38         {
39             dis[tr[x-1]]=dis[x]+1;
40             if(tr[x-1]==ed) break;
41             q.push(tr[x-1]);
42         }
43         if(ny<m&&tr[x+1]!=-1&&dis[tr[x+1]]==-1)
44         {
45             dis[tr[x+1]]=dis[x]+1;
46             if(tr[x+1]==ed) break;
47             q.push(tr[x+1]);
48         }
49         if(nx>1&&tr[x-m]!=-1&&dis[tr[x-m]]==-1)
50         {
51             dis[tr[x-m]]=dis[x]+1;
52             if(tr[x-m]==ed) break;
53             q.push(tr[x-m]);
54         }
55         if(nx<n&&tr[x+m]!=-1&&dis[tr[x+m]]==-1)
56         {
57             dis[tr[x+m]]=dis[x]+1;
58             if(tr[x+m]==ed) break;
59             q.push(tr[x+m]);
60         }
61         q.pop();
62     }
63     printf("%d\n",dis[ed]);
64 }
65
66 int main()
67 {
68     scanf("%d%d",&n,&m);
69     memset(kx,-1,sizeof(kx));
70     memset(tr,0,sizeof(tr));
71     for(int i=1;i<=n;i++)
72     {
73         scanf("%s",s);
74         for(int j=0;j<m;j++)
75         {
76             int now=(i-1)*m+j+1;
77             if(s[j]==‘@‘) st=now;
78             else if(s[j]==‘=‘) ed=now;
79             else if(s[j]==‘#‘) tr[now]=-1;
80             else if(s[j]>=‘A‘&&s[j]<=‘Z‘)
81             {
82                 int kk=s[j]-‘A‘+1;
83                 if(kx[kk]==-1) kx[kk]=now;
84                 else tr[now]=kx[kk],tr[kx[kk]]=now;
85             }
86         }
87     }
88     for(int i=1;i<=n*m;i++) if(tr[i]==0) tr[i]=i;
89     spfa();
90     return 0;
91 }

时间: 2024-10-25 20:43:48

【bzoj 3299】 [USACO2011 Open]Corn Maze玉米迷宫(最短路)的相关文章

3299: [USACO2011 Open]Corn Maze玉米迷宫

3299: [USACO2011 Open]Corn Maze玉米迷宫 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 137  Solved: 59[Submit][Status][Discuss] Description 今年秋天,约翰带着奶牛们去玩玉米迷宫.迷宫可分成NxM个格子,有些格子种了玉 米,种宥玉米的格子无法通行. 迷宫的四条边界上都是种了玉米的格子,其屮只有一个格子 没种,那就是出口. 在这个迷宫里,有一些神奇的传送点6每个传送点

[USACO2011 Open]Corn Maze玉米迷宫

Time Limit: 10 Sec Memory Limit: 128 MB Description 今年秋天,约翰带着奶牛们去玩玉米迷宫.迷宫可分成NxM个格子,有些格子种了玉 米,种宥玉米的格子无法通行.迷宫的四条边界上都是种了玉米的格子,其屮只有一个格子 没种,那就是出口.在这个迷宫里,有一些神奇的传送点6每个传送点由一对点组成,一旦 走入传送点的某个结点,机器就会强制把你送到传送点的另一头去.所有的传送点都是双向 的,如果你定到了另一头,机器也会把你送回来.奶牛在一个单位的时间内只能向

洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze

P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in

[USACO11OPEN]玉米田迷宫Corn Maze

题目描述 This past fall, Farmer John took the cows to visit a corn maze. But this wasn't just any corn maze: it featured several gravity-powered teleporter slides, which cause cows to teleport instantly from one point in the maze to another. The slides w

【luogu P1825 [USACO11OPEN]玉米田迷宫Corn Maze】 题解

题目链接:https://www.luogu.org/problemnew/show/P1825 带有传送门的迷宫问题 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 2001; char map[maxn][maxn]; int fx[4] = {0, 1, 0, -1};

[uva11OPEN]玉米田迷宫Corn Maze(广搜bfs)

第一次做MLE了…第二次WA了5个点,其实就是一个判断错了…QAQ总的来说…是个水题/板子题(逃 #include<bits/stdc++.h> using namespace std; #define For(i,l,r) for(register int i=l; i<r; i++) int n,m; int d[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0 }; bool bj,vis[301][301]; char ch[301][301]; struct

[LeetCode] The Maze III 迷宫之三

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r), but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. T

[LeetCode] The Maze II 迷宫之二

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. Given the ball's 

bzoj 3594: [Scoi2014]方伯伯的玉米田 dp树状数组优化

3594: [Scoi2014]方伯伯的玉米田 Time Limit: 60 Sec  Memory Limit: 128 MBSubmit: 314  Solved: 132[Submit][Status] Description 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美.这排玉米一共有N株,它们的高度参差不齐.方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感的玉米拔除掉,使得剩下的玉米的高度构成一个单调不下降序列.方伯伯可以选择一个区间,把这个区间的