LETTERS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total 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.
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.
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 6 HFDFFB AJHGDH DGAGEH
Sample Output
6
非常水的DFS,数组开的小了,WA一次。
Z的ascii码为90,所以数组大小肯定大于90咯
#include <iostream> #include <cstring> #include <algorithm> using namespace std; #pragma warning(disable : 4996) #define MAX 100 char 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码表,它的全称是“美国信息交换标准代码”。 |
八进制 | 十六进制 | 十进制 | 字符 | 八进制 | 十六进制 | 十进制 | 字符 |
---|---|---|---|---|---|---|---|
00 | 00 | 0 | nul | 100 | 40 | 64 | @ |
01 | 01 | 1 | soh | 101 | 41 | 65 | A |
02 | 02 | 2 | stx | 102 | 42 | 66 | B |
03 | 03 | 3 | etx | 103 | 43 | 67 | C |
04 | 04 | 4 | eot | 104 | 44 | 68 | D |
05 | 05 | 5 | enq | 105 | 45 | 69 | E |
06 | 06 | 6 | ack | 106 | 46 | 70 | F |
07 | 07 | 7 | bel | 107 | 47 | 71 | G |
10 | 08 | 8 | bs | 110 | 48 | 72 | H |
11 | 09 | 9 | ht | 111 | 49 | 73 | I |
12 | 0a | 10 | nl | 112 | 4a | 74 | J |
13 | 0b | 11 | vt | 113 | 4b | 75 | K |
14 | 0c | 12 | ff | 114 | 4c | 76 | L |
15 | 0d | 13 | er | 115 | 4d | 77 | M |
16 | 0e | 14 | so | 116 | 4e | 78 | N |
17 | 0f | 15 | si | 117 | 4f | 79 | O |
20 | 10 | 16 | dle | 120 | 50 | 80 | P |
21 | 11 | 17 | dc1 | 121 | 51 | 81 | Q |
22 | 12 | 18 | dc2 | 122 | 52 | 82 | R |
23 | 13 | 19 | dc3 | 123 | 53 | 83 | S |
24 | 14 | 20 | dc4 | 124 | 54 | 84 | T |
25 | 15 | 21 | nak | 125 | 55 | 85 | U |
26 | 16 | 22 | syn | 126 | 56 | 86 | V |
27 | 17 | 23 | etb | 127 | 57 | 87 | W |
30 | 18 | 24 | can | 130 | 58 | 88 | X |
31 | 19 | 25 | em | 131 | 59 | 89 | Y |
32 | 1a | 26 | sub | 132 | 5a | 90 | Z |
33 | 1b | 27 | esc | 133 | 5b | 91 | [ |
34 | 1c | 28 | fs | 134 | 5c | 92 | \ |
35 | 1d | 29 | gs | 135 | 5d | 93 | ] |
36 | 1e | 30 | re | 136 | 5e | 94 | ^ |
37 | 1f | 31 | us | 137 | 5f | 95 | _ |
40 | 20 | 32 | sp | 140 | 60 | 96 | ' |
41 | 21 | 33 | ! | 141 | 61 | 97 | a |
42 | 22 | 34 | " | 142 | 62 | 98 | b |
43 | 23 | 35 | # | 143 | 63 | 99 | c |
44 | 24 | 36 | $ | 144 | 64 | 100 | d |
45 | 25 | 37 | % | 145 | 65 | 101 | e |
46 | 26 | 38 | & | 146 | 66 | 102 | f |
47 | 27 | 39 | ` | 147 | 67 | 103 | g |
50 | 28 | 40 | ( | 150 | 68 | 104 | h |
51 | 29 | 41 | ) | 151 | 69 | 105 | i |
52 | 2a | 42 | * | 152 | 6a | 106 | j |
53 | 2b | 43 | + | 153 | 6b | 107 | k |
54 | 2c | 44 | , | 154 | 6c | 108 | l |
55 | 2d | 45 | - | 155 | 6d | 109 | m |
56 | 2e | 46 | . | 156 | 6e | 110 | n |
57 | 2f | 47 | / | 157 | 6f | 111 | o |
60 | 30 | 48 | 0 | 160 | 70 | 112 | p |
61 | 31 | 49 | 1 | 161 | 71 | 113 | q |
62 | 32 | 50 | 2 | 162 | 72 | 114 | r |
63 | 33 | 51 | 3 | 163 | 73 | 115 | s |
64 | 34 | 52 | 4 | 164 | 74 | 116 | t |
65 | 35 | 53 | 5 | 165 | 75 | 117 | u |
66 | 36 | 54 | 6 | 166 | 76 | 118 | v |
67 | 37 | 55 | 7 | 167 | 77 | 119 | w |
70 | 38 | 56 | 8 | 170 | 78 | 120 | x |
71 | 39 | 57 | 9 | 171 | 79 | 121 | y |
72 | 3a | 58 | : | 172 | 7a | 122 | z |
73 | 3b | 59 | ; | 173 | 7b | 123 | { |
74 | 3c | 60 | < | 174 | 7c | 124 | | |
75 | 3d | 61 | = | 175 | 7d | 125 | } |
76 | 3e | 62 | > | 176 | 7e | 126 | ~ |
77 | 3f | 63 | ? | 177 | 7f | 127 | del |
ASCII码表 | |||||||
信息在计算机上是用二进制表示的,这种表示法让人理解就很困难。因此计算机上都配有输入和输出设备,这些设备的主要目的就是,以一种人类可阅读的形式将信息在这些设备上显示出来供人阅读理解。为保证人类和设备,设备和计算机之间能进行正确的信息交换,人们编制的统一的信息交换代码,这就是ASCII码表,它的全称是“美国信息交换标准代码”。 |
八进制 | 十六进制 | 十进制 | 字符 | 八进制 | 十六进制 | 十进制 | 字符 |
---|---|---|---|---|---|---|---|
00 | 00 | 0 | nul | 100 | 40 | 64 | @ |
01 | 01 | 1 | soh | 101 | 41 | 65 | A |
02 | 02 | 2 | stx | 102 | 42 | 66 | B |
03 | 03 | 3 | etx | 103 | 43 | 67 | C |
04 | 04 | 4 | eot | 104 | 44 | 68 | D |
05 | 05 | 5 | enq | 105 | 45 | 69 | E |
06 | 06 | 6 | ack | 106 | 46 | 70 | F |
07 | 07 | 7 | bel | 107 | 47 | 71 | G |
10 | 08 | 8 | bs | 110 | 48 | 72 | H |
11 | 09 | 9 | ht | 111 | 49 | 73 | I |
12 | 0a | 10 | nl | 112 | 4a | 74 | J |
13 | 0b | 11 | vt | 113 | 4b | 75 | K |
14 | 0c | 12 | ff | 114 | 4c | 76 | L |
15 | 0d | 13 | er | 115 | 4d | 77 | M |
16 | 0e | 14 | so | 116 | 4e | 78 | N |
17 | 0f | 15 | si | 117 | 4f | 79 | O |
20 | 10 | 16 | dle | 120 | 50 | 80 | P |
21 | 11 | 17 | dc1 | 121 | 51 | 81 | Q |
22 | 12 | 18 | dc2 | 122 | 52 | 82 | R |
23 | 13 | 19 | dc3 | 123 | 53 | 83 | S |
24 | 14 | 20 | dc4 | 124 | 54 | 84 | T |
25 | 15 | 21 | nak | 125 | 55 | 85 | U |
26 | 16 | 22 | syn | 126 | 56 | 86 | V |
27 | 17 | 23 | etb | 127 | 57 | 87 | W |
30 | 18 | 24 | can | 130 | 58 | 88 | X |
31 | 19 | 25 | em | 131 | 59 | 89 | Y |
32 | 1a | 26 | sub | 132 | 5a | 90 | Z |
33 | 1b | 27 | esc | 133 | 5b | 91 | [ |
34 | 1c | 28 | fs | 134 | 5c | 92 | \ |
35 | 1d | 29 | gs | 135 | 5d | 93 | ] |
36 | 1e | 30 | re | 136 | 5e | 94 | ^ |
37 | 1f | 31 | us | 137 | 5f | 95 | _ |
40 | 20 | 32 | sp | 140 | 60 | 96 | ' |
41 | 21 | 33 | ! | 141 | 61 | 97 | a |
42 | 22 | 34 | " | 142 | 62 | 98 | b |
43 | 23 | 35 | # | 143 | 63 | 99 | c |
44 | 24 | 36 | $ | 144 | 64 | 100 | d |
45 | 25 | 37 | % | 145 | 65 | 101 | e |
46 | 26 | 38 | & | 146 | 66 | 102 | f |
47 | 27 | 39 | ` | 147 | 67 | 103 | g |
50 | 28 | 40 | ( | 150 | 68 | 104 | h |
51 | 29 | 41 | ) | 151 | 69 | 105 | i |
52 | 2a | 42 | * | 152 | 6a | 106 | j |
53 | 2b | 43 | + | 153 | 6b | 107 | k |
54 | 2c | 44 | , | 154 | 6c | 108 | l |
55 | 2d | 45 | - | 155 | 6d | 109 | m |
56 | 2e | 46 | . | 156 | 6e | 110 | n |
57 | 2f | 47 | / | 157 | 6f | 111 | o |
60 | 30 | 48 | 0 | 160 | 70 | 112 | p |
61 | 31 | 49 | 1 | 161 | 71 | 113 | q |
62 | 32 | 50 | 2 | 162 | 72 | 114 | r |
63 | 33 | 51 | 3 | 163 | 73 | 115 | s |
64 | 34 | 52 | 4 | 164 | 74 | 116 | t |
65 | 35 | 53 | 5 | 165 | 75 | 117 | u |
66 | 36 | 54 | 6 | 166 | 76 | 118 | v |
67 | 37 | 55 | 7 | 167 | 77 | 119 | w |
70 | 38 | 56 | 8 | 170 | 78 | 120 | x |
71 | 39 | 57 | 9 | 171 | 79 | 121 | y |
72 | 3a | 58 | : | 172 | 7a | 122 | z |
73 | 3b | 59 | ; | 173 | 7b | 123 | { |
74 | 3c | 60 | < | 174 | 7c | 124 | | |
75 | 3d | 61 | = | 175 | 7d | 125 | } |
76 | 3e | 62 | > | 176 | 7e | 126 | ~ |
77 | 3f | 63 | ? | 177 | 7f | 127 | del |