TAG 水题 模拟
按题意模拟就行。不过注意能阻挡queue的是已有的棋子,而不是会被攻击的格子。用bool数组不够表示所有状态。我用char。
board[i][j]= {
0, (空格,安全)
1, (会被攻击)
2, (已有题目给的棋子)
}
读取所有数据后再处理吧。。
发贴时我的代码居然在status的第一位。。喵。。
#include <stdio.h> #include<memory.h> const int N=1000; const int KX[]={-2, -1, 1, 2, 2, 1, -1, -2}; const int KY[]={1, 2, 2, 1, -1, -2, -2, -1}; const int QX[]={-1, -1, 0, 1, 1, 1, 0, -1}; const int QY[]={0, 1, 1, 1, 0, -1, -1, -1}; char board[N][N]; int n,m; int knight[100][2]; int queue[100][2]; int pawn[100][2]; int nk,nq,np; int casenum; int ans; bool checkRange(int r,int c) { if ( r<0 || r>=n || c<0 || c>=m ) return false; if ( board[r][c]==2 ) { return false; }else { return true; } } void putPiece(int r,int c, int type) { switch( type ) { case 1: for (int i=0; i<8; ++i) { int rr,cc; rr=r+KX[i]; cc=c+KY[i]; if ( checkRange(rr,cc) && board[rr][cc]==0 ) { --ans; board[rr][cc]=1; } } break; case 2: for (int i=0; i<8; ++i) { int rr,cc; rr=r+QX[i]; cc=c+QY[i]; while ( true ) { if ( checkRange(rr,cc) ) { if ( board[rr][cc]==0 ) { --ans; board[rr][cc]=1; } } else break; rr=rr+QX[i]; cc=cc+QY[i]; } }break; case 3: break; } } int main(int argc, char** argv) { casenum=0; while ( scanf("%d%d",&n, &m), n+m!=0 ) { ++casenum; memset(board, 0, sizeof(board) ); ans=n*m; scanf("%d", &nq); for (int i=0; i<nq; ++i) { scanf("%d%d", &queue[i][0], &queue[i][1]); board[ queue[i][0]-1 ] [ queue[i][1]-1 ]=2; --ans; } scanf("%d", &nk); for (int i=0; i<nk; ++i) { scanf("%d%d", &knight[i][0], &knight[i][1]); board[ knight[i][0]-1 ] [ knight[i][1]-1 ]=2; --ans; } scanf("%d", &np); for (int i=0; i<np; ++i) { scanf("%d%d", &pawn[i][0], &pawn[i][1]); board[ pawn[i][0]-1 ] [ pawn[i][1]-1 ]=2; --ans; } /*for (int i=0; i<np; ++i) { putPiece(pawn[i][0]-1,pawn[i][1]-1,3); }*/ for (int i=0; i<nk; ++i) { putPiece(knight[i][0]-1,knight[i][1]-1,1); } for (int i=0; i<nq; ++i) { putPiece(queue[i][0]-1,queue[i][1]-1,2); } /*for (int i=0; i<n; ++i) { for (int j=0; j<m; ++j) printf("%d", board[i][j]); printf("/n"); }*/ printf("Board %d has %d safe squares./n",casenum, ans); } return 0; }