P3956 [NOIP2017 普及组] 棋盘程序只有70分,大佬们求助,帮忙看看哪里错了
#include<bits/stdc++.h>
using namespace std;
int n, m, i, j, a[110][110], f[110][110], x, y, col;
int tag[10][10] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct no {
int x, y, col, flag; //x行y列 颜色col flag是否用魔法
};
queue<no> q;
int main () {
// freopen("chess.in", "r", stdin);
// freopen("chess.out", "w", stdout);
scanf("%d %d", &n, &m);
while(m--)
scanf("%d %d %d", &x, &y, &col), a[x][y] = col + 1;
memset(f, 125, sizeof(f));
q.push((no){1, 1, a[1][1], 0}); f[1][1] = 0;
while(!q.empty ()) {
no cur = q.front(), nxt; q.pop(); //cur当前的 nxt下一个
for (i = 0; i < 4; ++i){
nxt.x = cur.x + tag[i][0];
nxt.y = cur.y + tag[i][1];
if(nxt.x < 1 || nxt.y < 1 || nxt.x > n || nxt.y > n)
continue;
nxt.col = a[nxt.x][nxt.y]; nxt.flag = 0;
int k; //花费
if(nxt.col == 0)
if(!cur.flag)
k = 2, nxt.col = cur.col, nxt.flag = 1;
else k = -1;
else if(nxt.col == cur.col)
k = 0;
else k = 1;
if(k == -1) continue;
if(f[nxt.x][nxt.y] > f[cur.x][cur.y] + k)
f[nxt.x][nxt.y] = f[cur.x][cur.y] + k,
q.push(nxt);
}
}
printf("%d", f[n][n] == f[0][0] ? -1 : f[n][n]);
}