首页 > 其他分享 >hdu-1010

hdu-1010

时间:2023-03-03 13:06:57浏览次数:44  
标签:hdu int yy xx && ans include 1010

简单深搜 剪枝

http://acm.hdu.edu.cn/showproblem.php?pid=1010

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string.h>
#include <queue>
#include <sstream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

int n,m;
int t;

char p[10][10];
int ok ;
int sx,sy,ex,ey;

int dir[4][2]={ {0,1},{0,-1},{1,0},{-1,0}};

void dfs (int x,int y,int ans )
{
	if (ans == t)//判断时间是否用尽
	{
		if (x == ex && y == ey)
			ok = 1;
		return ;
	}
	if (ok)
		return ;

	int temp = abs(x-ex) + abs(y - ey) - abs(ans - t);//剪枝(重要)

	if ( temp > 0 || temp % 2  )
		return ;

	for(int i=0;i<4;i++)
	{
		int xx,yy;
		xx = x + dir[i][0];
		yy = y + dir[i][1];
		if (xx >=0 && xx <n && yy >=0 && yy< m && p[xx][yy] != 'X')
		{
			p[xx][yy] = 'X';
			dfs (xx,yy,ans+1);
			p[xx][yy] = '.';//回溯
		}

	}
}


int main ()
{
	while (cin>>n>>m>>t)
	{
		if (n==0 && m==0 && t==0)
			return 0;

		for (int i=0;i<10;i++)
			for(int j=0;j<10;j++)
			{
				p[i][j] = 'X';
			}


		for(int i=0;i<n;i++)
			cin>>p[i];

		int  tt = 0;
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
			{
				if ( p[i][j] == 'S' )
				{
					sx=i;
					sy=j;
				}
				if ( p[i][j] == 'D' )
				{
					ex= i;
					ey =j;
				}
				if ( p[i][j] == 'X' )		
					tt++;
			}

		if (  m*n - tt <= t )//如果时间大于最大步数 不符合条件
		{
			cout<<"NO"<<endl;
		}
		else 
		{
			ok = 0 ;
			p[sx][sy] = 'X';// 因为这里WA了好几次
			dfs (sx,sy,0);

			if (ok)
				cout<<"YES"<<endl;
			else
				cout<<"NO"<<endl;
		}
	}
	return 0;
}




标签:hdu,int,yy,xx,&&,ans,include,1010
From: https://blog.51cto.com/u_15990681/6098458

相关文章

  • hdu-1301
    模板题#include<iostream>#defineINF999999usingnamespacestd;intmap[30][30],dis[30],v[30];intprim(intn){inti,j,k,min,sum=0;for(i=1;i<=n......
  • hdu-1495
    bfs六种状态 #include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include<algorithm>#include<vector>#include<st......
  • hdu-2614
    取得第一个是第一个任务,时间0,接着进行下一个任务。#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<ctype.h>#include<alg......
  • hdu-1195
    http://acm.hdu.edu.cn/showproblem.php?pid=1195bfs加1减1交换,三个方式#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#includ......
  • hdu-1016
    约瑟夫换问题http://acm.hdu.edu.cn/showproblem.php?pid=1016#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<string.h>intn,cas=1,vi......
  • hdu-1238
    http://acm.hdu.edu.cn/showproblem.php?pid=1238SubstringsTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalS......
  • hdu-1515
    dfs 题意:给你两个字符串,问:第一个字符串按入栈出栈规则,能否达到第二个字符串,输出所有的方法,i表示入栈,o表示出栈。用dfs模拟第一个字符串入栈出栈过程:1.当前字符......
  • hdu-1548
    搜索做着做着成最短路径了。。dij本层可以直接到达的层数距离为1否则为无穷大#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#includ......
  • hdu-1253
    http://acm.hdu.edu.cn/showproblem.php?pid=1253这道水题#include<stdio.h>#include<iostream>#include<math.h>#include<stdlib.h>#include<......
  • hdu-2821
    http://acm.hdu.edu.cn/showproblem.php?pid=2821不要被题目吓到,认真读题还是好理解的#include<stdio.h>#include<iostream>#include<string.h>#include<math.h......