LETTERS(搜索题)
来源:互联网 发布:阿里云费用 编辑:程序博客网 时间:2024/06/04 19:50
LETTERS
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
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 6HFDFFBAJHGDHDGAGEH
Sample Output
6#include <iostream>#include <cstdio>#include <cstring>using namespace std;int r, c, ans;const int move[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};bool VisLetter[26], VisPos[21][21];char board[21][21];bool Judge(int i, int j){ return (!VisLetter[board[i][j] - 'A'] && !VisPos[i][j] && i >= 0 && i < r && j >= 0 && j < c);}void DFS(int i, int j, int cnt){ VisLetter[board[i][j] - 'A'] = VisPos[i][j] = 1; for(int k = 0; k < 4; k++) { int ii = i + move[k][0]; int jj = j + move[k][1]; if(Judge(ii, jj)) { DFS(ii, jj, cnt + 1); VisLetter[board[ii][jj] - 'A'] = VisPos[ii][jj] = 0; } } if(ans < cnt) ans = cnt;}int main(){ while(scanf("%d %d", &r, &c) != EOF) { ans = 1; for(int i = 0; i < r; i++) scanf("%s", board[i]); memset(VisLetter, 0, sizeof(VisLetter)); memset(VisPos, 0, sizeof(VisPos)); DFS(0, 0, 1); printf("%d\n", ans); }}
- LETTERS(搜索题)
- LETTERS(搜索)dfs
- 156:LETTERS(2.5基本算法之搜索)
- LETTERS
- 1050. Numbers & Letters (dfs)
- leetcode :316 Remove Duplicate Letters : 贪心+递归搜索
- C++搜索与回溯算法之LETTERS(字母)
- 49.Sort Letters by Case-字符大小写排序(中等题)
- poj 1154 LETTERS dfs入门题
- poj 1154 LETTERS(DFS水!)
- LeetCode 316 Remove Duplicate Letters(贪心)
- FCC--Missing letters(丢失的字母)
- OpenJudge 2.5-156 LETTERS(字母)
- LeetCode 316. Remove Duplicate Letters(删除重复字母)
- [BZOJ2789][Poi2012]Letters(树状数组求逆序对)
- 搭建Physical Review Letters(PRL)Latex编辑环境
- CodeForces 708A. Letters Cyclic Shift (字符串处理,贪心)
- Business Letters
- [笔记]Makefile wildcard
- DMA Test Driver for Linux2.6.xx+ARM9
- JAVA中 CallableStatement调用存储过程
- c#中stringbuilder的使用(转载学习)
- ARRAY_SIZE以及__must_be_array(arr)) --来源于网络
- LETTERS(搜索题)
- [Unity3D]手机3D游戏开发:如何实现最高分的存储与显示(五)----使用TextField 输入并调整排名
- smarty二级导航栏
- 判断一个数是几位数 数组
- C# 注释及说明
- Uva 10891 Game of Sum(二维动规)
- SSH整合中遇到的问题整理
- 期待asterisk 12
- 经典文章:1-10