hdu 1045 要求全部逐一搜索完的深搜

#include<stdio.h>

#include<string.h>

int visit[10][10];
char map[10][10];
int n,ans,ss,t;

int judge(int x,int y)
{
int k;
if(x<0||x>=n||y<0||y>=n)
return 0;
if(visit[x][y]==1||map[x][y]==‘X‘)
return 0;
for(k=x-1;k>=0;k--)
{
if(map[k][y]==‘X‘)//判断这一行中间是否有X
break;
if(visit[k][y]==1)
{
return 0;
}
}
for(k=y-1;k>=0;k--)
{
if(map[x][k]==‘X‘)//判断这一列中间是否有X
break;
if(visit[x][k]==1)
return 0;
}
return 1;//严格的筛选!
}

void dfs(int ss)// ss表示此时搜索到第n个方格
{
int x,y;
if(ss==n*n)
{
if(ans<t)
ans=t;
return ;
}
x=ss/n;y=ss%n;
if(judge(x,y))
{
t++;
visit[x][y]=1;
dfs(ss+1);
t--;
visit[x][y]=0;
dfs(ss+1);
}
else
dfs(ss+1);
}

int main()
{
int i;
while(scanf("%d",&n),n)
{
t=0;
for(i=0;i<n;i++)
scanf("%s",map[i]);
memset(visit,0,sizeof(visit));
ans=0;
dfs(0);
printf("%d\n",ans);
}
return 0;
}

时间: 2024-10-10 15:35:20

hdu 1045 要求全部逐一搜索完的深搜的相关文章

HDU 4771 Stealing Harry Potter&#39;s Precious (深搜+广搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题面: 欢迎参加--BestCoder周年纪念赛(高质量题目+多重奖励) Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2207    Accepted Submis

hdu 5311 Hidden String dp o(n)算法 深搜

Hidden String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 857    Accepted Submission(s): 322 Problem Description Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets

HDU 1045 - Fire Net (最大独立集)

题意:给你一个正方形棋盘.每个棋子可以直线攻击,除非隔着石头.现在要求所有棋子都不互相攻击,问最多可以放多少个棋子. 这个题可以用搜索来做.每个棋子考虑放与不放两种情况,然后再判断是否能互相攻击来剪枝.最后取可以放置的最大值. 这里我转化成求最大独立集来做. 首先将每个空地编号,对于每个空地,与该位置可以攻击到的空地连边.找最多的空地使得不互相攻击,即求该图的最大独立集.与搜索做法基本一致,但是说法略有不同. 1 #include<iostream> 2 #include<cstring

hdu 1045 Fire Net(最小覆盖点+构图(缩点))

http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1045 Description Suppose that we have a square city with straight streets. A map of a city

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

HDU 1312 Red and Black (搜索)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 4970 Killing Monsters 【搜索】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4970 题目大意:给你一些防御塔的位置和其攻击力,以及一些怪物的血量和位置,问走到终点还有几个怪物活着. 题目思路:最开始看题目的时候就是区间更新的过程觉得会花很多时间,然后用的树状数组,后来发现用的一个机智方法可以过,简单了很多. 区间更新的主要时间是花在塔的伤害,(L,R)D伤害上面,我们用一个sttack数组记录伤害,在attack[L]+=D,在attack[R]-=D,然后从前往后扫一遍,可

hdu 1198 Farm Irrigation (搜索或并查集)

Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5818    Accepted Submission(s): 2521 Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle