SRM467人工智能算法题,火灾中的逃跑

7 篇文章 0 订阅

Problem Statement
    
NOTE: This problem statement contains images that may not display properly if viewed outside of the applet.   "Maze on Fire" was a not-so-popular electronic game that consisted of a maze built on a grid. Each cell in the grid is empty or contains a wall. Some of the empty cells initially contain fire, and fire propagates after each turn. A playable character is located somewhere in the maze, and its objective is to survive for as many turns as possible before it ends up in a cell that contains fire (at which point, the machine will play a "BURNED!" sound).  Your task is simple enough. You must write an artificial intelligence program that can control the character in such a way that it always survives for as many turns as possible. You are given a vector <string> that represents the maze. The j-th character of the i-th element of maze represents the cell at row i, column j. Each cell is one of the following: 
'.' : An empty cell.
'F' : A cell containing fire.
'#' : A wall.
'$' : The character.
 The game is played as follows. During each turn, the character may stay in its current cell or move to an adjacent empty cell which is not on fire. Two cells are considered adjacent if they share a side. After the character's turn, the fire will propagate. Each cell that contains fire in the current turn will set fire to all of its adjacent empty cells. If the cell in which the character is located catches fire, the game will end (and the current turn will count toward the total number of turns survived by the character). Return the maximum possible number of turns the character can survive, or -1 if it is possible for the character to survive indefinitely.
Definition
    
Class:
MazeOnFire
Method:
maximumTurns
Parameters:
vector <string>
Returns:
int
Method signature:

int maximumTurns(vector <string> maze)

(be sure your method is public)
    


Constraints
-
maze will contain between 1 and 50 elements, inclusive.
-
Each element of maze will contain between 2 and 50 characters, inclusive.
-
All elements of maze will contain the same number of characters.
-
Each element of maze will contain only '.', '$', '#' or 'F'.
-
maze will contain exactly one '$' character.
Examples
0)


    
{"F..",
 ".$.",
 "..."}
Returns: 4
The best move in this case is to take the character to the bottom right cell. Fire will reach that cell after the fourth turn. The four turns are detailed in the following picture:   
1)


    
{".F#...",
 "F....#",
 ".F###.",
 "F.#.$.",
 "F.#..."}
Returns: -1
There is a wall barrier between the character and the multiple cells that are on fire. It is possible to survive indefinitely.
2)


    
{"....#.",
 "$##.#.",
 ".#..#F",
 ".F#.#.",
 "..#..."}
Returns: 7


3)


    
{"...$..",
 "..#...",
 "..###.",
 "..#...",
 "F.#.F."}
Returns: 7
Sometimes it is best not to move the character at all.
4)


    
{".F....F.",
 ".#.##.#.",
 ".#....#.",
 "F.$##..F",
 ".#....#.",
 ".###.##.",
 ".F....F."}

Returns: 4





Returns: 4


#include <string>
#include <vector>
#include <iostream>
using namespace std;

class MazeOnFire
{
public:
	int maximumTurns( vector<string> maze )
	{	
		int initI = 0, initJ = 0;
		init( maze, initI, initJ );
		return calculateMaxTurns( maze, initI, initJ );
	}
	
	int calculateMaxTurns( vector<string> maze, int i, int j )
	{
		if( i < 0 || j < 0 || i >= maze.size() || j >= maze[i].size() || maze[i][j] == 'F' || maze[i][j] == '#' )
		{
			return 0;
		}
		bool canPropagate = propagate( maze );
		if( !canPropagate )
		{
			return -1;
		}
		int max = 0;
		int currentMove; 
		currentMove = calculateMaxTurns( maze, i, j );
		if( currentMove < 0 ) return -1;
		if( currentMove > max ) max = currentMove;
		currentMove = calculateMaxTurns( maze, i-1, j );
		if( currentMove < 0 ) return -1;
		if( currentMove > max ) max = currentMove;
		currentMove = calculateMaxTurns( maze, i, j-1 );
		if( currentMove < 0 ) return -1;
		if( currentMove > max ) max = currentMove;
		currentMove = calculateMaxTurns( maze, i+1, j );
		if( currentMove < 0 ) return -1;
		if( currentMove > max ) max = currentMove;
		currentMove = calculateMaxTurns( maze, i, j+1 );
		if( currentMove < 0 ) return -1;
		if( currentMove > max ) max = currentMove;
		return max + 1;
	}
	
	void init( vector<string>& maze, int& i, int& j )
	{
		
		int rows = maze.size();
		for( i = 0; i < rows ; i++ )
		{
			int cols = maze[i].size();
			for( j = 0; j < cols; j++ )
			{
				if( maze[i][j] == '$' )
				{
					maze[i][j] = '.';
					return;
				}
			}
		}
	}
	
	bool propagate( vector<string>& maze )
	{
		int rows = maze.size();
		bool propagated = false;
		for( int i = 0; i < rows ; i++ )
		{
			int cols = maze[i].size();
			for( int j = 0; j < cols; j++ )
			{
				if( maze[i][j] == '.' && (
					( i > 0 && maze[i-1][j] == 'F') ||
					( j > 0 && maze[i][j-1] == 'F') ||
					( i + 1 < rows && maze[i+1][j] == 'F') ||
					( j + 1 < cols && maze[i][j+1] == 'F') ) )
				{
					propagated = true;
					maze[i][j] = 'f';
				}
			}
		}
		for( int i = 0; i < rows ; i++ )
		{
			int cols = maze[i].size();
			for( int j = 0; j < cols; j++ )
			{
				if( maze[i][j] == 'f' ) maze[i][j] = 'F';
			}
		}
		return propagated;
	}
};




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值