dfs

1.Fire net

题目:

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. 

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening. 

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets. 

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through. 

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways. 



Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration. 
InputThe input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file. 
OutputFor each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration. 
Sample Input
4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
5
1
5
2
4

思路:

简单dfs

代码:

#include<iostream>
using namespace std;
int ans=0;//已放碉堡数 
char a[4][4];//n最大为4 
int n;
bool isok(int c,int d)//是否可放置 ,无论是行列都是以离自己的距离近为优先考察 
{
	for(int i=c-1;i>=0;i--)//同一列前面几行 
	{
		if(a[i][d]=='Y') return false;//已放置过,同一列不可再放置 
		if(a[i][d]=='X') break;//同一列与墙之间无其他碉堡,满足 
	}
	for(int i=d-1;i>=0;i--)
	{
		if(a[c][i]=='Y') return false;//已放置过,同一行不可再放置 
		if(a[c][i]=='X') break;//同一行与墙之间无其他碉堡,满足 
	}
	return true;
}

void dfs(int num,int cnt)
{
	if(num==n*n)//正方形,最后一个格子序号是n*n-1,都 遍历完就更新碉堡数 
	{
	 if(cnt>ans)
	 	  ans=cnt;
	 return ;
	}
	int r=num/n;//行数 
	int c=num%n;//列数 
	if(a[r][c]=='.'&&isok(r,c))
	{
		a[r][c]='Y';//标记满足条件且已放置碉堡 
		dfs(num+1,cnt+1);//本地放碉堡,继续dfs下一个 
		a[r][c]='.';//还原 
	}
	dfs(num+1,cnt);//本地不放置,继续dfs下一个 
}
int main()
{
	while(~scanf("%d",&n)&&n)
	{
		getchar();//收换行符 
		for (int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			    cin>>a[i][j];
			getchar();
		}
		ans=0;//每一次都要更新,因为是全局变量,所以属于所有例子,每一次改变都是下一次的初始化值 
		
		dfs(0,0);//第一个0表示序号0,第二个表示目前已放置的碉堡数为0 
		cout<<ans<<endl;
	}
	return 0;
} 

2.  振兴中华

题目:

  小明参加了学校的趣味运动会,其中的一个项目是:跳格子。
    地上画着一些格子,每个格子里写一个字,如下所示:

从我做起振
我做起振兴
做起振兴中
起振兴中华

    比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。
    要求跳过的路线刚好构成“从我做起振兴中华”这句话。
    请你帮助小明算一算他一共有多少种可能的跳跃路线呢?

答案是一个整数,请通过浏览器直接提交该数字。
注意:不要提交解答过程,或其它辅助说明类的内容。

【分析】dfs,可以将汉字转成12345678,根据观察,上下左右四个方向其实考虑右和下就可以

【答案】35


代码:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int map[4][5]={
{1,2,3,4,5},
{2,3,4,5,6},
{3,4,5,6,7},
{4,5,6,7,8}
};

int dir[2][2]={{0,1},{1,0}};//右 和 下 两个方向 

struct path
{
	int x;
	int y;
}p[25];

int ans=0;

void dfs(int x,int y)
{
	if(x==3&&y==4)//只有map[3][4]这个位置是“华 ”,结束一次搜索 
	{
	    ans++;
	    return ;
	}
	for(int i=0;i<2;i++)
	{
		int nx=x+dir[i][0];
		int ny=y+dir[i][1];
		
		if(nx>=0&&nx<4&&ny>=0&&ny<5&&map[nx][ny]!=0)//满足条件 
		{
			int temp=map[nx][ny];//保留数据 
			map[nx][ny]=0;//经过过 
			dfs(nx,ny);
			map[nx][ny]=temp;
		
		}
	}
}

int main()
{
	dfs(0,0);
	printf("%d\n",ans);
	return 0;
}

如果需要遍历每一条合适的路径,代码如下:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int map[4][5]={
{1,2,3,4,5},
{2,3,4,5,6},
{3,4,5,6,7},
{4,5,6,7,8}
};

int dir[2][2]={{0,1},{1,0}};//右 和 下 两个方向 

struct path
{
	int x;
	int y;
}p[25];

int ans=0;

void dfs(int x,int y,int cur)
{
	if(x==3&&y==4)//只有map[3][4]这个位置是“华 ”,结束一次搜索 
	{
		
		//打印路径
		for(int i=0;i<cur;i++)
		{
		printf("%d %d",p[i].x,p[i].y);
		printf("\n"); 
	    }
	    ans++;
	    return ;
	}
	for(int i=0;i<2;i++)
	{
		int nx=x+dir[i][0];
		int ny=y+dir[i][1];
		
		if(nx>=0&&nx<4&&ny>=0&&ny<5&&map[nx][ny]!=0)//满足条件 
		{
			int temp=map[nx][ny];
			map[nx][ny]=0;//经过过 
			p[cur].x=nx;
			p[cur].y=ny;
			cur++;
			dfs(nx,ny,cur);
			map[nx][ny]=temp;//还原 
			cur--;
		}
	}
}

int main()
{
	p[0].x=0;
	p[0].y=0;
	dfs(0,0,1);
	printf("%d\n",ans);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值