zju 1002

// zju 1002
//

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;
int N = 4;
int Max = 0;
char a[5][5];
int judge(int row, int col)//测试该坐标是否可以放置 返回 0 可放置,返回 1 不可放置
{
	for (int i = row - 1; i >= 0; i--)//测试该行之前的位置
	{
		if (a[i][col] == '@')return 1;//不行
		if (a[i][col] == 'X')break;

	}
	for (int i = col - 1; i >= 0; i--)
	{
		if (a[row][i] == '@')return 1;
		if (a[row][i] == 'X')break;
	}
	return 0;
}
void DFS(int pos, int count)
{
	int row, col;
	if (pos == N*N)
	{
		if (Max < count)
		{
			Max = count; return;
		}
	}
	else
	{//计算出行列坐标
		row = pos / N;
		col = pos % N;
		//测试该位置是否可行
		if (!judge(row, col)&&a[row][col]=='.')
		{
			a[row][col] = '@';
			DFS(pos + 1, count+1);
			a[row][col] = '.';//恢复数据
		}
		DFS(pos + 1, count);//该位置不放置
	}
}

int main()
{
	
	while (scanf_s("%d",&N)!=EOF&&N)
	{
		memset(a, 0, sizeof(a));//快速清零
		for (int i = 0; i < N; i++)
		{

			for (int j = 0; j < N; j++)
			{
				//scanf_s("%c", a[i][j]);
				cin >> a[i][j];
			}
		}
		DFS(0, 0);
		cout << Max << endl;
		Max = 0;
	}
	return 0;
}

老师还说了一种改良算法,以后有空再写。。。又是只加了几句而已。。。

AC 记得#include <stdio.h>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值