UESTC_邱老师降临小行星 2015 UESTC Training for Search Algorithm & String<Problem B>
2015-05-12 13:12
597 查看
B - 邱老师降临小行星
Time Limit: 10000/5000MS (Java/Others) Memory Limit: 65536/65535KB (Java/Others)Submit Status
人赢邱老师和任何男生比,都是不虚的。有一天,邱老师带妹子(们)来到了一个N行M列平面的小行星。对于每一个着陆地点,邱老师总喜欢带着妹子这样走:假设着陆地点为(r0, c0),那么他们下一步只能选择相邻格点,向四周走,即(r0–1, c0), (r0 + 1, c0), (r0, c0–1)或(r0, c0 + 1)。之后的路程必须严格按照右转-前进-左转-前进-右转......的道路前行。但是由于邱老师很心疼妹子,所以崎岖的山脉不可以到达。当不能前进时必须要原路返回。如下图。
问,邱老师在哪里着陆可以游历这颗星球最多的土地,输出可能访问到的最多的格点数。
Input
第一行一个整数T, 0<T≤20,表示输入数据的组数。对于每组数据,第一行有两个整数N和M,分别表示行数和列数,0<N,M≤1000
下面N行,每行M个字符(0或1)。
1代表可到达的地方,0代表山脉(不可到达的地方)。
Output
对于每一组数据,输出一个整数后换行,表示选择某点着陆后,可能访问到的最多的格点数。Sample input and output
Sample Input | Sample Output |
---|---|
2 4 3 111 111 111 111 3 3 111 101 111 | 10 4 |
这是一道记忆化搜索题目,每个格子对应4种形态,每种形态又有2种形态,故共有8种状态.
f(i,j,k,m) -> 在格子(i,j) 时对应形态 k 的第 m 种状态最远可以走X步
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 1e3 + 50; int maxarrive[maxn][maxn][4][2],r,c; bool pass[maxn][maxn]; bool inmap(int x,int y) { if (x > r || x <= 0 || y > c || y <= 0 || !pass[x][y]) return false; return true; } /* 你拉着提琴,优雅美丽,眼神却逃避 */ int dfs(int x,int y,int turn,int st) { if (maxarrive[x][y][turn][st] != -1) return maxarrive[x][y][turn][st]; int &ans = maxarrive[x][y][turn][st]; if (!inmap(x,y)) return ans = 0; if (turn == 0) { if (st == 0) ans = dfs(x,y+1,0,1) + 1; else ans = dfs(x-1,y,0,0) + 1; } else if (turn == 1) { if (st == 0) ans = dfs(x+1,y,1,1) + 1; else ans = dfs(x,y+1,1,0) + 1; } else if (turn == 2) { if (st == 0) ans = dfs(x,y-1,2,1) + 1; else ans = dfs(x+1,y,2,0) + 1; } else if (turn == 3) { if (st == 0) ans = 1 + dfs(x-1,y,3,1); else ans = 1 + dfs(x,y-1,3,0); } return ans; } int main(int argc,char *argv[]) { int Case; scanf("%d",&Case); while(Case--) { memset(pass,true,sizeof(pass)); memset(maxarrive,-1,sizeof(maxarrive)); scanf("%d%d",&r,&c); char buffer[1500]; for(int i = 1 ; i <= r ; ++ i) { scanf("%s",buffer); for(int j = 0 ; j < c ; ++ j) if (buffer[j] == '0') pass[i][j+1] = false; } int ans = 0; for(int i = 1 ; i <= r ; ++ i) for(int j = 1 ; j <= c ; ++ j) { if (pass[i][j]) { int newans = 1; newans += dfs(i-1,j,0,0); // up newans += dfs(i+1,j,2,0); // down newans += dfs(i,j-1,3,0); // left newans += dfs(i,j+1,1,0); // right ans = max(ans,newans); } } printf("%d\n",ans); } return 0; }
相关文章推荐
- UESTC_秋实大哥の恋爱物语 2015 UESTC Training for Search Algorithm & String<Problem K>
- UESTC_Ferris Wheel String 2015 UESTC Training for Search Algorithm & String<Problem L>
- UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm & String<Problem C>
- UESTC_Palindromic String 2015 UESTC Training for Search Algorithm & String<Problem M>
- UESTC_韩爷的梦 2015 UESTC Training for Search Algorithm & String<Problem N>
- UESTC_基爷的中位数 2015 UESTC Training for Search Algorithm & String<Problem D>
- UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>
- UESTC_Eight Puzzle 2015 UESTC Training for Search Algorithm & String<Problem F>
- UESTC_Infected Land 2015 UESTC Training for Search Algorithm & String<Problem G>
- UESTC_王之迷宫 2015 UESTC Training for Search Algorithm & String<Problem A>
- UESTC_全都是秋实大哥 2015 UESTC Training for Search Algorithm & String<Problem J>
- UESTC_邱老师的脑残粉 2015 UESTC Training for Graph Theory<Problem D>
- 2015 UESTC Training for Search Algorithm & String - J - 全都是秋实大哥 【KMP】
- UESTC_邱老师选妹子 2015 UESTC Training for Dynamic Programming<Problem H>
- UESTC_邱老师选妹子(二) 2015 UESTC Training for Dynamic Programming<Problem I>
- 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】
- UESTC_邱老师看电影 2015 UESTC Training for Dynamic Programming<Problem F>
- UESTC_邱老师玩游戏 2015 UESTC Training for Dynamic Programming<Problem G>
- 2016 UESTC Training for Search Algorithm & String A - Xiper的奇妙历险(1) 八皇后问题、dfs
- UESTC_树上的距离 2015 UESTC Training for Graph Theory<Problem E>