习题4-3 黑白棋 UVa220

1.题目描述:点击打开链接

2.解题思路:本题是一道模拟题,要求模拟黑白棋的三种操作。对于这类题,肯定是分情况编写相应的函数处理。不过本题的难点在于如何方便地寻找到合法位置。思考了一会儿,可以枚举每一个空位置,同时利用一个向量数组方便来后续讨论,由于实现的细节较多,详细过程见代码注释。本题还有一个细节:在执行M命令时记得要更换当前的游戏者,题目中说了是两个人轮流下棋。

3.代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

typedef pair<int, int> P;
#define N 8
char g[N][N];
vector<P>pos;
char player;//当前玩家,设为全局变量,便于后续讨论
int dx[] = { -1, 1, 0, 0, -1, 1, -1, 1 };//利用向量数组,方便处理各种情况
int dy[] = { 0, 0, -1, 1, -1, 1, 1, -1 };
bool inside(int x, int y)
{
	if (x<0 || x>7 || y<0 || y>7)return false;
	return true;
}
bool find(int d,int x,int y, char c)//沿着路径dx[d],dy[d]寻找目标棋子
{
	int w = dx[d], v = dy[d], i = x, j = y;
	while (inside(i, j) && g[i][j] == 'W' + 'B' - c){ i += w; j += v; }
	if (inside(i, j) && g[i][j] == c)return true; 
	return false;
}
bool is_valid(char c, int x, int y)//判断该位置是否为合法放置位置
{
	if (c == 'W')
	{
		for (int d = 0; d < 8; d++)
		if (inside(x + dx[d], y + dy[d]) && g[x + dx[d]][y + dy[d]]=='B')
		{
			int i = x + dx[d], j = y + dy[d];
			if (find(d, i, j, 'W'))return true;
		}
		return false;
	}
	else
	{
		for (int d = 0; d < 8; d++)
		if (inside(x + dx[d], y + dy[d]) && g[x + dx[d]][y + dy[d]] == 'W')
		{
			int i = x + dx[d], j = y + dy[d];
			if (find(d, i, j, 'B'))return true;
		}
		return false;
	}
}
bool find_pos()//寻找合法放置位置
{
	pos.clear();
	for (int i = 0; i < 8; i++)
	for (int j = 0; j < 8; j++)
	if (g[i][j] == '-'&&is_valid(player, i, j))//枚举每一个位置
		pos.push_back(P(i + 1, j + 1));
	int len = pos.size();
	if (len>0)return true;
	else return false;
}
void place(int x, int y)//放置棋子
{
	if (player == 'W')
	{
		g[x][y] = 'W';
		for (int d = 0; d < 8;d++)
		if (inside(x + dx[d], y + dy[d]) && g[x + dx[d]][y + dy[d]] == 'B')
		{
			int i = x + dx[d], j = y + dy[d];
			if (find(d, i, j, 'W'))
			{
				int w = dx[d], v = dy[d];
				while (g[i][j] != 'W'){ g[i][j] = 'W'; i += w; j += v; }
			}
		}
	}
	else
	{
		g[x][y] = 'B';
		for (int d = 0; d < 8; d++)
		if (inside(x + dx[d], y + dy[d]) && g[x + dx[d]][y + dy[d]] == 'W')
		{
			int i = x + dx[d], j = y + dy[d];
			if (find(d, i, j, 'B'))
			{
				int w = dx[d], v = dy[d];
				while (g[i][j] != 'B'){ g[i][j] = 'B'; i += w; j += v; }
			}
		}
	}
}
void print_num()//打印黑白棋的个数
{
	int n1, n2;
	n1 = n2 = 0;
	for (int i = 0; i < 8;i++)
	for (int j = 0; j < 8;j++)
	if (g[i][j] == 'W')n1++;
	else if (g[i][j]=='B')n2++;
	printf("Black - %2d White - %2d\n", n2, n1);
}
void print_board()//打印当前棋盘
{
	for (int i = 0; i < 8; i++)
	for (int j = 0; j < 8; j++)
		printf("%c%s", g[i][j], j == 7 ? "\n":"");
}
int main()
{
	//freopen("t.txt", "r", stdin);
	int T;
	cin >> T;
	int rnd = 0;
	while (T--)
	{
		memset(g, '\0', sizeof(g));
		for (int i = 0; i < 8; i++)
			scanf("%s", g[i]);
		cin >> player;
		char cmd;
		if (rnd++)cout << endl;
		while (cin >> cmd)
		{
			if (cmd == 'L')
			{
				if (find_pos())
				{
					int len = pos.size();
					for (int i = 0; i < len; i++)
						printf("(%d,%d)%c", pos[i].first, pos[i].second, i == len - 1 ? '\n' : ' ');
				}
				else puts("No legal move.");
			}
			if (cmd == 'M')
			{
				char str[10];
				scanf("%s", str);
				int r, c;
				r = str[0] - '0' - 1, c = str[1] - '0' - 1;
				if (g[r][c]=='-'&&!is_valid(player, r, c)){ player = 'W' + 'B' - player;  }
				place(r, c);
				print_num();
				player = 'W' + 'B' - player;
			}
			if (cmd == 'Q')
			{
				print_board();
				break;
			}
		}
	}
	return 0;
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
======================================================================== MICROSOFT FOUNDATION CLASS LIBRARY : fir ======================================================================== AppWizard has created this fir application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your fir application. fir.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. fir.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CFirApp application class. fir.cpp This is the main application source file that contains the application class CFirApp. fir.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. fir.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\fir.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file fir.rc. res\fir.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// For the main frame window: MainFrm.h, MainFrm.cpp These files contain the frame class CMainFrame, which is derived from CFrameWnd and controls all SDI frame features. ///////////////////////////////////////////////////////////////////////////// AppWizard creates one document type and one view: firDoc.h, firDoc.cpp - the document These files contain your CFirDoc class. Edit these files to add your special document data and to implement file saving and loading (via CFirDoc::Serialize). firView.h, firView.cpp - the view of the document These files contain your CFirView class. CFirView objects are used to view CFirDoc objects. ///////////////////////////////////////////////////////////////////////////// Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named fir.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Visual C++ reads and updates this file. ///////////////////////////////////////////////////////////////////////////// Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system's current language, you will need to copy the corresponding localized resources MFC42XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC42DEU.DLL contains resources translated to German.) If you don't do this, some of the UI elements of your application will remain in the language of the operating system. /////////////////////////////////////////////////////////////////////////////

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值