"Memory search" Devils Tower

Here Insert Picture Description

Direct violence memory search order (abscissa, ordinate, the current energy, the number of monsters kill) to the state of the memory.

Direct look at the code:

#include <bits/stdc++.h>

using namespace std;
const int N = 12;

int h, n, m, Ans;
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
int f[N][N][N*N];
char a[N][N];

void dfs(int x,int y,int ene,int ans)
{
	if (ans >= f[x][y][ene]) return;
	f[x][y][ene] = ans;
	if (a[x][y] == 'E') {
		Ans = min(Ans,ans); 
		return;
	}
	for (int i=0;i<4;++i)
	{
		int nx = x+dx[i];
		int ny = y+dy[i];
		if (nx < 1 || nx > n || ny < 1 || ny > m || a[nx][ny] == '#') continue;
		if (a[nx][ny] =='C') a[nx][ny] = '.', dfs(nx,ny,ene+5,ans), a[nx][ny] = 'C';
		if (a[nx][ny] == 'M' && ene > 0) a[nx][ny] = '.', dfs(nx,ny,ene-1,ans+1), a[nx][ny] = 'M';
		if (a[nx][ny] == '.' || a[nx][ny] == 'E') dfs(nx,ny,ene,ans);
	} 
	return;
}

void work(void)
{
	Ans = INT_MAX;
	memset(f,30,sizeof f);
	for (int i=1;i<=n;++i)
	    for (int j=1;j<=m;++j)
		    cin>>a[i][j];
	for (int i=1;i<=n;++i)
	    for (int j=1;j<=m;++j)
		    if (a[i][j] == 'S') {
		    	dfs(i,j,h,0);
		    	break;
			}	
	if (Ans == INT_MAX) puts("Poor Warrior");
	else cout<<Ans<<endl;
}

int main(void)
{
	freopen("tower.in","r",stdin);
	freopen("tower.out","w",stdout);
	while (cin>>h>>n>>m) work();
	return 0;
} 

Guess you like

Origin blog.csdn.net/Ronaldo7_ZYB/article/details/94625906