HDU 5335 - Walk Out (DFS + 贪心)
2015-07-31 15:34
260 查看
题目:
http://acm.hdu.edu.cn/showproblem.php?pid=5335
题意:
n*m的01矩阵,起点(1,1),终点(n,m)。求出走出的路径得到的最小的二进制。
思路:
贪心策略:
一开始能走0就走0,dfs走到离终点最近的0。接下来的路径应该尽可能短,且0尽可能多。
本题有一个很巧妙的地方:遍历到离终点最近的0(x+y = tot),接下来的最优答案便是每一条斜线一个位置,注意到每条斜线的x+y相等,所以从 tot~n+m 去查找最优的位置。
AC.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#pragma comment(linker,"/STACK:1024000000,1024000000");
using namespace std;
int n, m;
char rec[1005][1005];
int f[1005][1005];
int tot;
int vis[1005][1005];
void dfs(int x, int y)
{
if(vis[x][y]) return;
vis[x][y] = 1;
if(rec[x][y] == '1') return;
f[x][y] = 1;
if(x+y > tot) tot = x+y;
if(x > 1) dfs(x-1, y);
if(x < n) dfs(x+1, y);
if(y > 1) dfs(x, y-1);
if(y < m) dfs(x, y+1);
}
void solve()
{
if(tot == n+m) {
printf("0\n");
return;
}
if(tot == 0) {
printf("1");
f[1][1] = 1;
tot = 2;
}
for(int i = tot; i < n+m; ++i) {
int flag = 1; //要赋值,可能没有进入循环。
for(int j = max(1, i-m); j <= min(n, i-1); ++j) {
if(f[j][i-j]) {
int x = j < n? rec[j+1][i-j] - '0': 1;
int y = i-j < m? rec[j][i-j+1] - '0': 1;
flag = min(flag, min(x, y));
}
}
for(int j = max(1, i-m); j <= min(n, i-1); ++j) {
if(f[j][i-j]) {
int x = j < n? rec[j+1][i-j] - '0': 1;
int y = i-j < m? rec[j][i-j+1] - '0': 1;
if(x == flag) f[j+1][i-j] = 1;
if(y == flag) f[j][i-j+1] = 1;
}
}
printf("%d", flag);
}
printf("\n");
}
void init()
{
memset(vis, 0, sizeof(vis));
memset(f, 0, sizeof(f));
tot = 0;
}
int main()
{
//freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%s", rec[i]+1);
}
init();
dfs(1, 1);
solve();
}
return 0;
}
http://acm.hdu.edu.cn/showproblem.php?pid=5335
题意:
n*m的01矩阵,起点(1,1),终点(n,m)。求出走出的路径得到的最小的二进制。
思路:
贪心策略:
一开始能走0就走0,dfs走到离终点最近的0。接下来的路径应该尽可能短,且0尽可能多。
本题有一个很巧妙的地方:遍历到离终点最近的0(x+y = tot),接下来的最优答案便是每一条斜线一个位置,注意到每条斜线的x+y相等,所以从 tot~n+m 去查找最优的位置。
AC.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#pragma comment(linker,"/STACK:1024000000,1024000000");
using namespace std;
int n, m;
char rec[1005][1005];
int f[1005][1005];
int tot;
int vis[1005][1005];
void dfs(int x, int y)
{
if(vis[x][y]) return;
vis[x][y] = 1;
if(rec[x][y] == '1') return;
f[x][y] = 1;
if(x+y > tot) tot = x+y;
if(x > 1) dfs(x-1, y);
if(x < n) dfs(x+1, y);
if(y > 1) dfs(x, y-1);
if(y < m) dfs(x, y+1);
}
void solve()
{
if(tot == n+m) {
printf("0\n");
return;
}
if(tot == 0) {
printf("1");
f[1][1] = 1;
tot = 2;
}
for(int i = tot; i < n+m; ++i) {
int flag = 1; //要赋值,可能没有进入循环。
for(int j = max(1, i-m); j <= min(n, i-1); ++j) {
if(f[j][i-j]) {
int x = j < n? rec[j+1][i-j] - '0': 1;
int y = i-j < m? rec[j][i-j+1] - '0': 1;
flag = min(flag, min(x, y));
}
}
for(int j = max(1, i-m); j <= min(n, i-1); ++j) {
if(f[j][i-j]) {
int x = j < n? rec[j+1][i-j] - '0': 1;
int y = i-j < m? rec[j][i-j+1] - '0': 1;
if(x == flag) f[j+1][i-j] = 1;
if(y == flag) f[j][i-j+1] = 1;
}
}
printf("%d", flag);
}
printf("\n");
}
void init()
{
memset(vis, 0, sizeof(vis));
memset(f, 0, sizeof(f));
tot = 0;
}
int main()
{
//freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) {
scanf("%s", rec[i]+1);
}
init();
dfs(1, 1);
solve();
}
return 0;
}
相关文章推荐
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 1000
- HDU 1001
- HDU 1622 Trees On The Level
- HDU 1063 Exponentiation
- Hdu5033
- HDU 1166 敌兵布阵
- HDU Rightmost Digit
- hdu 1002
- 贪心 hdu 1003
- hdu 1004