题目描述
有一个m×m的棋盘,棋盘上每一个格子可能是红色、黄色或没有任何颜色的。你现在要从棋盘的最左上角走到棋盘的最右下角。
任何一个时刻,你所站在的位置必须是有颜色的(不能是无色的),你只能向上、下、左、右四个方向前进。当你从一个格子走向另一个格子时,如果两个格子的颜色相同,那你不需要花费金币;如果不同,则你需要花费1个金币。
另外,你可以花费2个金币施展魔法让下一个无色格子暂时变为你指定的颜色。但这个魔法不能连续使用,而且这个魔法的持续时间很短,也就是说,如果你使用了这个魔法,走到了这个暂时有颜色的格子上,你就不能继续使用魔法;只有当你离开这个位置,走到一个本来就有颜色的格子上的时候,你才能继续使用这个魔法,而当你离开了这个位置(施展魔法使得变为有颜色的格子)时,这个格子恢复为无色。
现在你要从棋盘的最左上角,走到棋盘的最右下角,求花费的最少金币是多少?
输入输出格式
输入格式
数据的第一行包含两个正整数m,n,以一个空格分开,分别代表棋盘的大小,棋盘上有颜色的格子的数量。
接下来的n行,每行三个正整数x,y,c,分别表示坐标为(x,y)的格子有颜色c。其中c=1代表黄色,c=0代表红色。相邻两个数之间用一个空格隔开。棋盘左上角的坐标为(1,1),右下角的坐标为(m,n)。
棋盘上其余的格子都是无色。保证棋盘的左上角,也就是(1,1)一定是有颜色的。
输出格式
输出一行,一个整数,表示花费的金币的最小值。如果无法到达,输出-1。
输入输出样例
输入样例一:
5 7
1 1 0
1 2 0
2 2 1
3 3 1
3 4 0
4 4 1
5 5 0
输出样例一
8
输入样例二
5 5
1 1 0
1 2 0
2 2 1
3 3 1
5 5 0
输出样例二
-1
说明
样例说明
样例一说明:
样例二说明:
数据规模
对于30%的数据,1≤m≤5,1≤n≤10。
对于60%的数据,1≤m≤20,1≤n≤200。
对于100%的数据,1≤m≤100,1≤n≤1000。
题解
dijkstra模板,把所有情况列出即可。
#include <iostream> #include <algorithm> #define MAX_M 101 #define MAX_N 1001 #define WHITE 0 #define RED 1 #define YELLOR 2 #define INF 100000000 using namespace std; int m, n; struct Node { int x, y; int color; int dis; bool vis; }a[MAX_N]; void Update(); int Check(int, int); void Dijkstra(); int main() { cin >> m >> n; for(register int i = 1; i <= n; ++i) { cin >> a[i].x >> a[i].y >> a[i].color; ++a[i].color; } Update(); Dijkstra(); cout << (a[n].dis == INF ? -1 : a[n].dis); return 0; } void Dijkstra() { int prev, cost; for(register int i = 1; i <= n; ++i) { a[i].dis = INF; } a[1].dis = 0; for(register int I = 1; I <= n; ++I) { prev = -1; for(register int i = 1; i <= n; ++i) { if(a[i].vis) continue; if(prev == -1) prev = i; else if(a[i].dis < a[prev].dis) prev = i; } if(prev == -1) break; a[prev].vis = true; for(register int i = 1; i <= n; ++i) { if(a[i].vis) continue; cost = Check(prev, i); if(cost == -1) continue; if(a[prev].dis + cost < a[i].dis) { a[i].dis = a[prev].dis + cost; } } } return; } void Update() { for(register int i = 1; i <= n; ++i) { if(a[i].x == 1 && a[i].y == 1) { swap(a[i], a[1]); break; } } for(register int i = 1; i <= n; ++i) { if(a[i].x == m && a[i].y == m) { swap(a[i], a[n]); break; } else if(i == n) { ++n; a[n].x = a[n].y = m; a[n].color = WHITE; break; } } return; } inline int Check(int prev, int next) { int tip = ((a[prev].color == WHITE || a[next].color == WHITE) ? 2 : 0); // no white or only one white if(tip) { if(a[prev].x - 1 == a[next].x && a[prev].y == a[next].y) return tip; if(a[prev].x + 1 == a[next].x && a[prev].y == a[next].y) return tip; if(a[prev].x == a[next].x && a[prev].y - 1 == a[next].y) return tip; if(a[prev].x == a[next].x && a[prev].y + 1 == a[next].y) return tip; } else { tip = (a[prev].color != a[next].color); if(a[prev].x - 1 == a[next].x && a[prev].y == a[next].y) return tip; if(a[prev].x + 1 == a[next].x && a[prev].y == a[next].y) return tip; if(a[prev].x == a[next].x && a[prev].y - 1 == a[next].y) return tip; if(a[prev].x == a[next].x && a[prev].y + 1 == a[next].y) return tip; if(a[prev].x - 2 == a[next].x && a[prev].y == a[next].y) return tip + 2; if(a[prev].x + 2 == a[next].x && a[prev].y == a[next].y) return tip + 2; if(a[prev].x == a[next].x && a[prev].y - 2 == a[next].y) return tip + 2; if(a[prev].x == a[next].x && a[prev].y + 2 == a[next].y) return tip + 2; if(a[prev].x - 1 == a[next].x && a[prev].y - 1 == a[next].y) return tip + 2; if(a[prev].x - 1 == a[next].x && a[prev].y + 1 == a[next].y) return tip + 2; if(a[prev].x + 1 == a[next].x && a[prev].y - 1 == a[next].y) return tip + 2; if(a[prev].x + 1 == a[next].x && a[prev].y + 1 == a[next].y) return tip + 2; } return -1; }