栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【迫真题解】X子棋

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【迫真题解】X子棋

这题是大模拟。

写完了以后改成了游戏供大家娱乐。

具体功能读代码即可。

#include 
#include  
#define no printf("illegaln")
#define yes printf((i % 2 == 1 ? "Player1 winsn" : "Player2 winsn")), exit(0)
#define debug cout << "fuckn"
using namespace std;
int f[1007][1007], a[1007], x[1007], y[1007], ans[8], n, m, k, dir[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}};
bool vis[1007], dead[1007];
void gotoxy(int x, int y){
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
bool in(int x, int y){
	return x > 0 && x <= m && y > 0 && y <= m;
}
bool alive(int x, int y){
	for(int i = 0; i < 4; i++){
		int fx = x + dir[i][0], fy = y + dir[i][1];
		if(!in(fx, fy)) continue;
		if(!f[fx][fy]) return true;
	}
	return false;
}
struct nod{
	int x, y;
};
vector v;
bool bfs(int x, int y){
	bool flag = false;
	queue q;
	q.push({x, y});
	v.push_back(f[x][y]);
	vis[f[x][y]] = true;
	flag |= alive(x, y);
	while(!q.empty()){
		nod fr = q.front();
		q.pop();
		int cx = fr.x, cy = fr.y;
		flag |= alive(cx, cy);
		for(int i = 0; i < 4; i++){
			int fx = cx + dir[i][0], fy = cy + dir[i][1];
			if(!in(fx, fy) || !f[fx][fy] || vis[f[fx][fy]]) continue;
			if(f[fx][fy] % 2 == f[x][y] % 2){
				q.push({fx, fy});
				v.push_back(f[fx][fy]);
				vis[f[fx][fy]] = true;
			}
		}
	}
	return flag;
}
void print_board(){
	system("cls");
	for(int i = 1; i <= m; i++){
		for(int j = 0; j <= m; j++){
			gotoxy(3 * (i - 1) + 3, 2 * (j - 1));
			if(!j){
				gotoxy(3 * (i - 1) + 3, 2 * m);
				printf("%d", i);
				continue;
			}
			if(!f[j][i]) printf(".");
			else if(f[j][i] & 1) printf("#");
			else printf("$");
		}
	}
	for(int i = 1; i <= m; i++){
		gotoxy(0, 2 * (i - 1));
		printf("%d", i);
	}
	gotoxy(0, 2 * m + 5);
}
int main(){
	int typ;
	scanf("%d %d %d %d", &n, &m, &k, &typ);
	for(int i = 1; i <= n; i++){
		print_board();
		scanf("%d %d", &x[i], &y[i]);
		if(f[x[i]][y[i]]) no, i++, yes;
		memset(vis, false, sizeof(vis));
		f[x[i]][y[i]] = i;
		if(typ){
			for(int j = 1; j <= i; j++){
				if((i + 1 - j) & 1) continue;
				if(!vis[j] && !dead[j]){
					v.clear();
					bool res = bfs(x[j], y[j]);
					if(!res) for(int k = 0; k < v.size(); k++) f[x[v[k]]][y[v[k]]] = 0, dead[v[k]] = true;
				}
			}
			for(int j = 1; j <= i; j++){
				if((i - j) & 1) continue;
				if(!vis[j] && !dead[j]){
					v.clear();
					bool res = bfs(x[j], y[j]);
					if(!res) no, i++, yes;
				}
			}
		}
		memset(ans, 0, sizeof(ans));
		for(int j = 0; j < 8; j++){
			int cx = x[i], cy = y[i];
			for(int k = 0; ; k++){
				if(!in(cx, cy) || !f[cx][cy]){
					ans[j] = k;
					break;
				}
				if(f[cx][cy] % 2 != f[x[i]][y[i]] % 2){
					ans[j] = k;
					break;
				}
				cx += dir[j][0], cy += dir[j][1];
			}
		}
		for(int j = 0; j < 4; j++) if(ans[j << 1] + ans[(j << 1) + 1] > k) print_board(), yes;
	}
	printf("drawn");
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/302357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号