NOI 2.5 搜索 156: LETTERS

题目来源:http://noi.openjudge.cn/ch0205/156/

156:LETTERS

总时间限制: 10000ms   内存限制: 65536kB

总时间限制1000ms   内存限制65536kB

描述

A single-player game is played on a rectangular boarddivided in R rows and C columns. There is a single uppercase letter (A-Z)written in every position in the board.
Before the begging of the game there is a figure in the upper-left corner ofthe board (first row, first column). In every move, a player can move thefigure to the one of the adjacent positions (up, down,left or right). Onlyconstraint is that a figure cannot visit a position marked with the same lettertwice.
The goal of the game is to play as many moves as possible.
Write a program that will calculate the maximal number of positions in theboard the figure can visit in a single game.

输入

The first line of the input contains two integers R andC, separated by a single blank character, 1 <= R, S <= 20.
The following R lines contain S characters each. Each line represents one rowin the board.

输出

The first and only line of the output should contain themaximal number of position in the board the figure can visit.

样例输入

3 6
HFDFFB
AJHGDH
DGAGEH

样例输出

6

-----------------------------------------------------

解题思路

递归深搜。每一次递归取上下左右四个方向里的最大值。

-----------------------------------------------------

代码

//156:LETTERS
//总时间限制: 1000ms 内存限制: 65536kB
//描述
//A single-player game is played on a rectangular board divided in R rows and C columns. There is a single uppercase letter (A-Z) written in every position in the board.
//Before the begging of the game there is a figure in the upper-left corner of the board (first row, first column). In every move, a player can move the figure to the one of the adjacent positions (up, down,left or right). Only constraint is that a figure cannot visit a position marked with the same letter twice.
//The goal of the game is to play as many moves as possible.
//Write a program that will calculate the maximal number of positions in the board the figure can visit in a single game.
//输入
//The first line of the input contains two integers R and C, separated by a single blank character, 1 <= R, S <= 20.
//The following R lines contain S characters each. Each line represents one row in the board.
//输出
//The first and only line of the output should contain the maximal number of position in the board the figure can visit.
//样例输入
//3 6
//HFDFFB
//AJHGDH
//DGAGEH
//样例输出
//6

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

int r,c;
int mat[21][21] = {};

int walk(int x, int y, vector<bool> cnt)
{
	int ans = 1;
	if (x>0 && !cnt[mat[x-1][y]])
	{
		vector<bool> cnt1(cnt);
		cnt1[mat[x-1][y]] = 1;
		int ans1 = walk(x-1,y,cnt1)+1;
		ans = ans1>ans? ans1:ans;
	}
	if (y>0 && !cnt[mat[x][y-1]])
	{
		vector<bool> cnt1(cnt);
		cnt1[mat[x][y-1]] = 1;
		int ans1 = walk(x,y-1,cnt1)+1;
		ans = ans1>ans? ans1:ans;
	}
	if (x<r-1 && !cnt[mat[x+1][y]])
	{
		vector<bool> cnt1(cnt);
		cnt1[mat[x+1][y]] = 1;
		int ans1 = walk(x+1,y,cnt1)+1;
		ans = ans1>ans? ans1:ans;
	}
	if (y<c-1 && !cnt[mat[x][y+1]])
	{
		vector<bool> cnt1(cnt);
		cnt1[mat[x][y+1]] = 1;
		int ans1 = walk(x,y+1,cnt1)+1;
		ans = ans1>ans? ans1:ans;
	}
	return ans;
}


int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin("0205_156.txt");
	int i,j;
	char tmp;
	fin >> r >> c;
	for (i=0; i<r; i++)
	{
		for (j=0; j<c; j++)
		{
			fin >> tmp;
			mat[i][j] = (int)(tmp-'A');
		}
	}
	fin.close();
	vector<bool> cnt(26,0);				// 26个0
	cnt[mat[0][0]] = 1;
	cout << walk(0,0,cnt);
	return 0;
#endif
#ifdef ONLINE_JUDGE
	int i,j;
	char tmp;
	cin >> r >> c;
	for (i=0; i<r; i++)
	{
		for (j=0; j<c; j++)
		{
			cin >> tmp;
			mat[i][j] = (int)(tmp-'A');
		}
	}
	vector<bool> cnt(26,0);				// 26个0
	cnt[mat[0][0]] = 1;
	cout << walk(0,0,cnt);
	return 0;
#endif
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80460172
156
2.5