Codeforces Gym 100650D Queens, Knights and Pawns 暴力
原题链接:http://codeforces.com/gym/100650/attachments/download/3269/20052006-acmicpc-east-central-north-america-regional-contest-ecna-2005-en.pdf
题意
给你个棋盘,棋盘上有若干皇后、骑士和士兵,皇后和骑士会攻击,士兵不会,攻击规则满足国际象棋的规则,问你有多少格子是安全的。
题解
直接模拟就好了
代码
#include<iostream> #include<cstring> #include<cstdio> #define MAX_N 1003 using namespace std; int n,m; int B[MAX_N][MAX_N];//0 empty 1 queen 2 knight 3 pawn -1 unsafe int qu[MAX_N][2],t0; int kn[MAX_N][2],t1; int qdx[8]={0,1,0,-1,1,1,-1,-1},qdy[8]={1,0,-1,0,1,-1,1,-1}; int kdx[8]={2,1,-2,-1,2,1,-2,-1},kdy[8]={-1,-2,1,2,1,2,-1,-2}; void display(){ for(int i=0;i<n;i++,cout<<endl) for(int j=0;j<m;j++){ switch (B[i][j]){ case 0: cout<<'O'; break; case 1: cout<<'Q'; break; case 2: cout<<'K'; break; case 3: cout<<'P'; break; case -1: cout<<'#'; break; } } } int main() { int cas = 0; while (true) { scanf("%d%d", &n, &m); if (n == 0 && m == 0)break; memset(B, 0, sizeof(B)); scanf("%d", &t0); for (int i = 0; i < t0; i++) { scanf("%d%d", &qu[i][0], &qu[i][1]); qu[i][0]--;qu[i][1]--; B[qu[i][0]][qu[i][1]] = 1; } scanf("%d", &t1); for (int i = 0; i < t1; i++) { scanf("%d%d", &kn[i][0], &kn[i][1]); kn[i][0]--;kn[i][1]--; B[kn[i][0]][kn[i][1]] = 2; } int tp; scanf("%d", &tp); for (int i = 0; i < tp; i++) { int u, v; scanf("%d%d", &u, &v); u--;v--; B[u][v] = 3; } for (int i = 0; i < t0; i++) { int u = qu[i][0], v = qu[i][1]; for (int j = 0; j < 8; j++) for (int k = 1; ; k++) { int nx = u + k * qdx[j], ny = v + k * qdy[j]; if (nx >= n || nx < 0 || ny >= m || ny < 0 || B[nx][ny] > 0)break; B[nx][ny] = -1; } } for (int i = 0; i < t1; i++) { int u = kn[i][0], v = kn[i][1]; for (int j = 0; j < 8; j++) { int nx = u + kdx[j], ny = v + kdy[j]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || B[nx][ny] != 0)continue; B[nx][ny] = -1; } } //display(); int s = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (B[i][j] == 0)s++; printf("Board %d has %d safe squares.\n", ++cas, s); } return 0; }