⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2020.cpp

📁 这是哈尔滨工业大学acmOJ的源代码
💻 CPP
字号:
/*  This Code is Submitted by wywcgs for Problem 2020 on 2006-05-31 at 23:01:39 */ 
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;

const int N = 8;
const int DIR[][8][2] = { { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } },
						{ { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } }, { { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } },
						{ { 1, 2 }, { 1, -2 }, { 2, 1 }, { 2, -1 }, { -1, 2 }, { -1, -2 }, { -2, 1 }, { -2, -1 } }, 
						{ { 1, 1 }, { 1, -1 } }, { { -1, 1 }, { -1, -1 } } };
const char PIECE[] = "KpPNBRQ";
const int D[] = { 0, 4, 5, 3, 2, 1, 0 };
const int DN[] = { 8, 2, 2, 8, 4, 4, 8 };

char map[N][N], st[128];

bool legal(int x, int y) { return x >= 0 && x < N && y >= 0 && y < N && !isalpha(map[x][y]); }
char upper(char c) { if(islower(c)) c += 'A'-'a'; return c; }

int main()
{
	int i, j, k, l;

	while(gets(st) != NULL) {
		memset(map, '.', sizeof(map));
		char *p = st;
		for(i = 0; i < N; i++, p++)
			for(j = 0; j < N; p++)
				if(isdigit(*p)) j += *p - '0';
				else if(*p != 'P' && *p != 'p') map[i][j++] = upper(*p);
				else map[i][j++] = *p;
		for(i = 0; i < N; i++)
			for(j = 0; j < N; j++) {
				if(!isalpha(map[i][j])) continue;
				int o;
				for(o = 0; PIECE[o] != map[i][j]; o++) ;
				if(o < 4) {
					for(k = 0; k < DN[o]; k++) {
						int x = i+DIR[D[o]][k][0], y = j+DIR[D[o]][k][1];
						if(legal(x, y)) map[x][y] = '*';
					}
				} else {
					for(k = 0; k < DN[o]; k++)
						for(l = 1; l < N; l++) {
							int x = i+DIR[D[o]][k][0]*l, y = j+DIR[D[o]][k][1]*l;
							if(legal(x, y)) map[x][y] = '*';
							else break;
						}
				}
			}
		int safe = 0;
		for(i = 0; i < N; i++)
			for(j = 0; j < N; j++)
				if(map[i][j] == '.') safe++;
		printf("%d\n", safe);
	}
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -