P3956 [NOIP2017 普及组] 棋盘
理解
read() 快读函数:
(1)强类型语言C++ => 泛型汇编
template <typename Tp>
void read(Tp &x)
(2)&x 引用:实参 => 形参
actual parameter
(3)getchar() :遇 ’\n’ 止步
template <typename Tp>
void read(Tp &x) {
x = 0; int f = 1; char c = getchar();
while (c < '0' || c > '9') {
if(c == '-')f = -1; c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0'; c = getchar();
}
x *= f;
}
for (int i = 0; i < N; i++) {
read(X), read(Y), read(C);
a[X][Y] = C + 1;//+1:区分无色情况0
}
优先队列:默认值(最大值优先)
(1)结构体 + 重载操作符(const: 重载标志)
bool operator < (node b) const {
return p > b.p;
}
struct node {
int x, y, c, p;
bool operator < (node b) const {
return p > b.p;
}
};
priority_queue<node> q;
int dx[]={0,0,1,0,-1,1,1,-1,-1,0,2,0,-2};
int dy[]={0,1,0,-1,0,1,-1,1,-1,2,0,-2,0};
int dc[]={0,0,0,0,0,2,2,2,2,2,2,2,2};//施法代价+2
while (!q.empty()) {
cur = q.top(); q.pop();
if (cost[cur.x][cur.y] < cur.p)continue; //是否开始
for (int i = 1; i <= 12; i++) {
nxt.x = cur.x + dx[i];
nxt.y = cur.y + dy[i];
if (nxt.x <= 0 || nxt.x > M || nxt.y <= 0 || nxt.y > M) continue;//边界判断
nxt.c = a[nxt.x][nxt.y];
nxt.p = cur.p + dc[i];
if (!nxt.c) continue;//落点颜色判断:无色?
if (nxt.c != cur.c) nxt.p++;//前后颜色对比:异?(代价+1)
if (cost[nxt.x][nxt.y] > nxt.p) {
cost[nxt.x][nxt.y] = nxt.p;
q.push(nxt);
}
}
}
终止条件判断:
if (!a[M][M]) {//无色
int cnt = min(cost[M][M - 1], cost[M - 1][M]) + 2;//*
if (cnt >= inf) printf("-1");
else printf("%d\n", cnt);
}
else {//有色
if (cost[M][M] == inf) printf("-1");
else printf("%d\n", cost[M][M]);
}
题目
题目背景
NOIP2017 普及组 T3
题目描述
有一个m \times mm×m的棋盘,棋盘上每一个格子可能是红色、黄色或没有任何颜色的。你现在要从棋盘的最左上角走到棋盘的最右下角。
任何一个时刻,你所站在的位置必须是有颜色的(不能是无色的), 你只能向上、 下、左、 右四个方向前进。当你从一个格子走向另一个格子时,如果两个格子的颜色相同,那你不需要花费金币;如果不同,则你需要花费 11个金币。
另外, 你可以花费 22 个金币施展魔法让下一个无色格子暂时变为你指定的颜色。但这个魔法不能连续使用, 而且这个魔法的持续时间很短,也就是说,如果你使用了这个魔法,走到了这个暂时有颜色的格子上,你就不能继续使用魔法; 只有当你离开这个位置,走到一个本来就有颜色的格子上的时候,你才能继续使用这个魔法,而当你离开了这个位置(施展魔法使得变为有颜色的格子)时,这个格子恢复为无色。
现在你要从棋盘的最左上角,走到棋盘的最右下角,求花费的最少金币是多少?
输入格式
第一行包含两个正整数m, nm,n,以一个空格分开,分别代表棋盘的大小,棋盘上有颜色的格子的数量。
接下来的nn行,每行三个正整数x, y, cx,y,c, 分别表示坐标为(x,y)(x,y)的格子有颜色cc。
其中c=1c=1 代表黄色,c=0c=0 代表红色。 相邻两个数之间用一个空格隔开。 棋盘左上角的坐标为(1, 1)(1,1),右下角的坐标为( m, m)(m,m)。
棋盘上其余的格子都是无色。保证棋盘的左上角,也就是(1, 1)(1,1) 一定是有颜色的。
输出格式
一个整数,表示花费的金币的最小值,如果无法到达,输出-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
输出 #1复制
8
输入 #2复制
5 5
1 1 0
1 2 0
2 2 1
3 3 1
5 5 0
输出 #2复制
-1
数据规模与约定
对于 30%30%的数据, 1 ≤ m ≤ 5, 1 ≤ n ≤ 101≤m≤5,1≤n≤10。
对于 60%60%的数据, 1 ≤ m ≤ 20, 1 ≤ n ≤ 2001≤m≤20,1≤n≤200。
对于 100%100%的数据, 1 ≤ m ≤ 100, 1 ≤ n ≤ 1,0001≤m≤100,1≤n≤1,000。
代码
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
int M, N, X, Y, C, a[105][105], cost[105][105];
int dx[]={0,0,1,0,-1,1,1,-1,-1,0,2,0,-2};
int dy[]={0,1,0,-1,0,1,-1,1,-1,2,0,-2,0};
int dc[]={0,0,0,0,0,2,2,2,2,2,2,2,2};
template <typename Tp>
void read(Tp &x) {
x = 0; int f = 1; char c = getchar();
while (c < '0' || c > '9') {
if(c == '-')f = -1; c = getchar();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0'; c = getchar();
}
x *= f;
}
struct node {
int x, y, c, p;
bool operator < (node b) const {
return p > b.p;
}
};
priority_queue<node> q;
void bfs() {
memset(cost, 0x3f, sizeof(cost)); cost[1][1] = 0;
q.push((node){1, 1, a[1][1], cost[1][1]});
node cur, nxt;
while (!q.empty()) {
cur = q.top(); q.pop();
if (cost[cur.x][cur.y] < cur.p)continue;
for (int i = 1; i <= 12; i++) {
nxt.x = cur.x + dx[i];
nxt.y = cur.y + dy[i];
if (nxt.x <= 0 || nxt.x > M || nxt.y <= 0 || nxt.y > M) continue;
nxt.c = a[nxt.x][nxt.y];
nxt.p = cur.p + dc[i];
if (!nxt.c) continue;
if (nxt.c != cur.c) nxt.p++;
if (cost[nxt.x][nxt.y] > nxt.p) {
cost[nxt.x][nxt.y] = nxt.p;
q.push(nxt);
}
}
}
}
int main() {
read(M), read(N);
for (int i = 0; i < N; i++) {
read(X), read(Y), read(C);
a[X][Y] = C + 1;
}
bfs();
if (!a[M][M]) {
int cnt = min(cost[M][M - 1], cost[M - 1][M]) + 2;
if (cnt >= inf) printf("-1");
else printf("%d\n", cnt);
}
else {
if (cost[M][M] == inf) printf("-1");
else printf("%d\n", cost[M][M]);
}
return 0;
}