sicily 1172.Queens,Knights and Pawns

来源:互联网 发布:win7 添加打印机 端口 编辑:程序博客网 时间:2024/05/23 15:10
#include <iostream>#include <string>#include <vector>#include <memory.h>using namespace std;struct board{bool have;bool visited;};struct xy{int x;int y;};board Board[1005][1005];vector<xy> queen;vector<xy> knight;vector<xy> pawn;int row, col, k_num, p_num, q_num;int k_stepx[8] = {-2, -2, -1, 1, 2, 2, 1, -1};int k_stepy[8] = {-1, 1, 2, 2, 1, -1, -2, -2};int q_stepx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};int q_stepy[8] = {0, 1, 1, 1, 0, -1, -1, -1};void cal_queen()//扫描queen会攻击的点{for(int i = 0;i < queen.size();i++){for(int j = 0;j < 8;j++){int x = queen[i].x + q_stepx[j];int y = queen[i].y + q_stepy[j];while(1){if(x <= 0 || x > row || y <= 0 || y > col|| Board[x][y].have)//1、判断边界 2、queen在没有阻挡的情况下可以一直走下去,所以用have判断该点是否已经被占据{break;}else{Board[x][y].visited = true;x = x + q_stepx[j];y = y + q_stepy[j];}}}}}void cal_knight()//扫描knight会攻击的点{for(int i = 0;i < knight.size();i++){for(int j = 0;j < 8;j++){int x = knight[i].x + k_stepx[j];int y = knight[i].y + k_stepy[j];if(x <= 0 || x > row || y <= 0 || y > col)continue;Board[x][y].visited = true;}}}int main(){int caseid = 0;while(cin >> row >> col && row != 0 && col != 0){caseid++;memset(Board,false,sizeof(Board));queen.clear();knight.clear();pawn.clear();xy temp;cin >> q_num;for(int i = 0;i < q_num;i++){cin >> temp.x >> temp.y;queen.push_back(temp);Board[temp.x][temp.y].have = true;Board[temp.x][temp.y].visited = true;}cin >> k_num;for(int i = 0;i < k_num;i++){cin >> temp.x >> temp.y;knight.push_back(temp);Board[temp.x][temp.y].have = true;Board[temp.x][temp.y].visited = true;}cin >> p_num;for(int i = 0;i < p_num;i++){cin >> temp.x >> temp.y;pawn.push_back(temp);Board[temp.x][temp.y].have = true;Board[temp.x][temp.y].visited = true;}cal_queen();cal_knight();int count = 0;for(int i = 1;i <= row;i++)for(int j = 1;j <= col;j++)if(!Board[i][j].visited)count++;cout << "Board " << caseid << " has " << count << " safe squares." << endl;}return 0;}