标签:
直接模拟就好了,好像没有什么巧办法~
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 struct point { 6 int x; 7 int y; 8 }q[105], k[105]; 9 10 int ans; 11 char board[1005][1005]; 12 13 void judge_queen(int num, int n, int m) 14 { 15 int move_x[]={-1,-1,-1,0,0,1,1,1}; 16 int move_y[]={1,0,-1,1,-1,1,0,-1}; 17 for(int i=0; i<num; i++) 18 { 19 for(int j=0; j<8; j++) 20 { 21 int tempx = q[i].x; 22 int tempy = q[i].y; 23 tempx += move_x[j]; 24 tempy += move_y[j]; 25 while(tempx >= 1 && tempx <= n && tempy >= 1 && tempy <= m) 26 { 27 if(board[tempx][tempy] != ‘K‘ && board[tempx][tempy] != ‘P‘ && board[tempx][tempy] != ‘Q‘) 28 { 29 if(board[tempx][tempy] != ‘1‘) 30 { 31 board[tempx][tempy] = ‘1‘; 32 ans++; 33 } 34 } 35 else 36 break; 37 tempx += move_x[j]; 38 tempy += move_y[j]; 39 } 40 } 41 } 42 } 43 44 void judge_knight(int num, int n, int m) 45 { 46 int move_x[]={-2,-2,-1,-1,1,1,2,2}; 47 int move_y[]={1,-1,2,-2,2,-2,1,-1}; 48 for(int i=0; i<num; i++) 49 { 50 for(int j=0; j<8; j++) 51 { 52 int tempx = k[i].x + move_x[j]; 53 int tempy = k[i].y + move_y[j]; 54 //cout << tempx << " " << tempy << endl; 55 if(tempx >= 1 && tempx <= n && tempy >= 1 && tempy <= m) 56 { 57 if(board[tempx][tempy] != ‘P‘ && board[tempx][tempy] != ‘K‘ && board[tempx][tempy] != ‘Q‘ && board[tempx][tempy] != ‘1‘) 58 { 59 board[tempx][tempy] = ‘1‘; 60 ans++; 61 } 62 } 63 } 64 } 65 } 66 67 void print(int n, int m) 68 { 69 for(int i=1; i<=n; i++) 70 { 71 for(int j=1; j<=m; j++) 72 cout << board[i][j] << " "; 73 cout << endl; 74 } 75 } 76 77 int main() 78 { 79 int n, m; 80 int count=1; 81 while(scanf("%d%d", &n, &m) != EOF, n, m) 82 { 83 memset(board, 0, sizeof(board)); 84 ans=0; 85 int tot=0; 86 int qnum, knum, pnum, xx, yy; 87 scanf("%d", &qnum); 88 for(int i=0; i<qnum; i++) 89 { 90 scanf("%d%d", &q[i].x, &q[i].y); 91 board[q[i].x][q[i].y] = ‘Q‘; 92 } 93 scanf("%d", &knum); 94 for(int i=0; i<knum; i++) 95 { 96 scanf("%d%d", &k[i].x, &k[i].y); 97 board[k[i].x][k[i].y] = ‘K‘; 98 } 99 scanf("%d", &pnum); 100 for(int i=0; i<pnum; i++) 101 { 102 scanf("%d%d", &xx, &yy); 103 board[xx][yy] = ‘P‘; 104 } 105 tot = tot + qnum + knum + pnum; 106 //print(n, m); 107 judge_queen(qnum, n, m); 108 judge_knight(knum, n, m); 109 printf("Board %d has %d safe squares.\n", count++, n*m-ans-tot); 110 } 111 112 return 0; 113 }
sicily 1172 Queens, Knights and Pawns
标签:
原文地址:http://www.cnblogs.com/dominjune/p/4569903.html