UVALive 6585 Draughts (DFS回溯法 )

33 篇文章 0 订阅

大体题意:

给你一个10×10的棋盘,有白棋和黑棋,它们只能落在#上,白棋要想吃黑棋,必须相邻对角线有个黑棋,并且黑棋通用的对角线位置不能有棋,求最多吃多少?

思路:

直接回溯搜索即可!不用vis数组,改变棋盘,在回溯回来即可!

详细见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ps push_back
#define mr make_pair
#define fi first
#define se second
using namespace std;
char s[17][17];
vector<pair<int,int> >v;
int ans = 0;
const int dx[] = {1,-1,1,-1};
const int dy[] = {1,-1,-1,1};
bool init(int x,int y){
	return x >= 0 && x < 10 && y >= 0 && y < 10;
}
void dfs(int x,int y,int cur){
	bool ok = 0;
	for (int i = 0 ; i < 4 ; ++i){
		int xx = dx[i] + x;
		int yy = dy[i] + y;
		int xx2 = dx[i]*2 + x;
		int yy2 = dy[i]*2 + y;
		if (!init(xx,yy) || !init(xx2,yy2)  )continue;
		if (s[xx2][yy2] != '#')continue;
		if (s[xx][yy] != 'B')continue;
		ok = 1;
		char ch = s[x][y];
		s[x][y] = '#';
		s[xx][yy] = '#';
		dfs(xx2,yy2,cur+1);
		s[xx][yy] = 'B';
		s[x][y] = ch;
	}
	if (!ok) ans = max(ans,cur);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		v.clear();
		for (int i = 0; i < 10; ++i){
			scanf("%s",s[i]);
			for (int j = 0; j < 10; ++j){
				if (s[i][j] == 'W')v.ps(mr(i,j));
			}
		}
		int len = v.size();
		ans = 0;
		for (int i = 0; i < len; ++i){
			s[v[i].fi][v[i].se] = '#';
			dfs(v[i].fi,v[i].se,0);
			s[v[i].fi][v[i].se] = 'W';
		}
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值