poj_1154LETTERS

来源:互联网 发布:淘宝万艾可货到付款 编辑:程序博客网 时间:2024/06/16 16:46

LETTERS
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5278 Accepted: 2518

Description

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.

Input

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.

Output

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

Sample Input

3 6HFDFFBAJHGDHDGAGEH

Sample Output

6

非常水的DFS,数组开的小了,WA一次。

Z的ascii码为90,所以数组大小肯定大于90咯

#include <iostream>#include <cstring>#include <algorithm>using namespace std;#pragma warning(disable : 4996)#define MAX 100char map[MAX][MAX];bool vis[MAX];const int moves[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};int n, m;int ans;void dfs(int x, int y, int cnt){for(int i = 0; i < 4; i++){int p = x + moves[i][0];int q = y + moves[i][1];if(p >=1 && p <= n && q >= 1 && q <= m && !vis[map[p][q]-'0']){vis[map[p][q]-'0'] = true;dfs(p, q, cnt + 1);vis[map[p][q]-'0'] = false;}}ans = max(ans, cnt);}int main(){freopen("in.txt", "r", stdin);cin >> n >> m;for(int i = 1; i <= n; i++){for(int j = 1; j <= m; j++){cin >> map[i][j];}}memset(vis, false, sizeof(vis));vis[map[1][1]-'0'] = true;ans = -1;dfs(1, 1, 1);cout << ans << endl;}


ASCII码表

信息在计算机上是用二进制表示的,这种表示法让人理解就很困难。因此计算机上都配有输入和输出设备,这些设备的主要目的就是,以一种人类可阅读的形式将信息在这些设备上显示出来供人阅读理解。为保证人类和设备,设备和计算机之间能进行正确的信息交换,人们编制的统一的信息交换代码,这就是ASCII码表,它的全称是“美国信息交换标准代码”。

 

八进制十六进制十进制字符八进制十六进制十进制字符00000nul1004064@01011soh1014165A02022stx1024266B03033etx1034367C04044eot1044468D05055enq1054569E06066ack1064670F07077bel1074771G10088bs1104872H11099ht1114973I120a10nl1124a74J130b11vt1134b75K140c12ff1144c76L150d13er1154d77M160e14so1164e78N170f15si1174f79O201016dle1205080P211117dc11215181Q221218dc21225282R231319dc31235383S241420dc41245484T251521nak1255585U261622syn1265686V271723etb1275787W301824can1305888X311925em1315989Y321a26sub1325a90Z331b27esc1335b91[341c28fs1345c92\351d29gs1355d93]361e30re1365e94^371f31us1375f95_402032sp1406096'412133!1416197a422234"1426298b432335#1436399c442436$14464100d452537%14565101e462638&14666102f472739`14767103g502840(15068104h512941)15169105i522a42*1526a106j532b43+1536b107k542c44,1546c108l552d45-1556d109m562e46.1566e110n572f47/1576f111o603048016070112p613149116171113q623250216272114r633351316373115s643452416474116t653553516575117u663654616676118v673755716777119w703856817078120x713957917179121y723a58:1727a122z733b59;1737b123{743c60<1747c124|753d61=1757d125}763e62>1767e126~773f63?1777f127del


ASCII码表

信息在计算机上是用二进制表示的,这种表示法让人理解就很困难。因此计算机上都配有输入和输出设备,这些设备的主要目的就是,以一种人类可阅读的形式将信息在这些设备上显示出来供人阅读理解。为保证人类和设备,设备和计算机之间能进行正确的信息交换,人们编制的统一的信息交换代码,这就是ASCII码表,它的全称是“美国信息交换标准代码”。

 

八进制十六进制十进制字符八进制十六进制十进制字符00000nul1004064@01011soh1014165A02022stx1024266B03033etx1034367C04044eot1044468D05055enq1054569E06066ack1064670F07077bel1074771G10088bs1104872H11099ht1114973I120a10nl1124a74J130b11vt1134b75K140c12ff1144c76L150d13er1154d77M160e14so1164e78N170f15si1174f79O201016dle1205080P211117dc11215181Q221218dc21225282R231319dc31235383S241420dc41245484T251521nak1255585U261622syn1265686V271723etb1275787W301824can1305888X311925em1315989Y321a26sub1325a90Z331b27esc1335b91[341c28fs1345c92\351d29gs1355d93]361e30re1365e94^371f31us1375f95_402032sp1406096'412133!1416197a422234"1426298b432335#1436399c442436$14464100d452537%14565101e462638&14666102f472739`14767103g502840(15068104h512941)15169105i522a42*1526a106j532b43+1536b107k542c44,1546c108l552d45-1556d109m562e46.1566e110n572f47/1576f111o603048016070112p613149116171113q623250216272114r633351316373115s643452416474116t653553516575117u663654616676118v673755716777119w703856817078120x713957917179121y723a58:1727a122z733b59;1737b123{743c60<1747c124|753d61=1757d125}763e62>1767e126~773f63?1777f127del