ZOJ_4127 Grid with Arrows(欧拉路径 || 动态规划)

Grid with Arrows

Time Limit: 1000 ms
Memory Limit: 65536 KB
Problem Description

BaoBao has just found a grid with n n n rows and m m m columns in his left pocket, where the cell in the j j j-th column of the i i i-th row (indicated by ( i , j ) (i, j) (i,j)) contains an arrow (pointing either upwards, downwards, leftwards or rightwards) and an integer a i , j a_{i, j} ai,j.

BaoBao decides to play a game with the grid. He will first select a cell as the initial cell and tick it. After ticking a cell (let’s say BaoBao has just ticked cell ( i , j ) (i, j) (i,j)), BaoBao will go on ticking another cell according to the arrow and the integer in cell ( i , j ) (i, j) (i,j).

  • If the arrow in cell ( i , j ) (i, j) (i,j) points upwards, BaoBao will go on ticking cell ( i − a i , j , j ) (i-a_{i, j}, j) (iai,j,j) if it exists.
  • If the arrow in cell ( i , j ) (i, j) (i,j) points downwards, BaoBao will go on ticking cell ( i + a i , j , j ) (i+a_{i, j}, j) (i+ai,j,j) if it exists.
  • If the arrow in cell ( i , j ) (i, j) (i,j) points leftwards, BaoBao will go on ticking cell ( i , j − a i , j ) (i, j-a_{i, j}) (i,jai,j) if it exists.
  • If the arrow in cell ( i , j ) (i, j) (i,j) points rightwards, BaoBao will go on ticking cell ( i , j + a i , j ) (i, j+a_{i, j}) (i,j+ai,j) if it exists.

If the cell BaoBao decides to tick does not exist, or if the cell is already ticked, the game ends.

BaoBao is wondering if he can select a proper initial cell, so that he can tick every cell in the grid exactly once before the game ends. Please help him find the answer.

Input

There are multiple test cases. The first line contains an integer T T T, indicating the number of test cases. For each test case:

The first line contains two integers n n n and m m m ( 1 ≤ n × m ≤ 1 0 5 1 \le n \times m \le 10^5 1n×m105), indicating the number of rows and columns of the grid.

For the following n n n lines, the i i i-th line contains a string s i s_i si consisting of lowercased English letters ( ∣ s i ∣ = m |s_i| = m si=m, s i , j ∈ { ’u’ (ascii: 117) , ’d’ (ascii: 100) , ’l’ (ascii: 108) , ’r’ (ascii: 114) } s_{i, j} \in \{\text{'u' (ascii: 117)}, \text{'d' (ascii: 100)}, \text{'l' (ascii: 108)}, \text{'r' (ascii: 114)}\} si,j{’u’ (ascii: 117),’d’ (ascii: 100),’l’ (ascii: 108),’r’ (ascii: 114)}), where s i , j s_{i, j} si,j indicates the direction of arrow in cell ( i , j ) (i, j) (i,j).

  • If s i , j = ’u’ s_{i, j} = \text{'u'} si,j=’u’, the arrow in cell ( i , j ) (i, j) (i,j) points upwards.
  • If s i , j = ’d’ s_{i, j} = \text{'d'} si,j=’d’, the arrow in cell ( i , j ) (i, j) (i,j) points downwards.
  • If s i , j = ’l’ s_{i, j} = \text{'l'} si,j=’l’, the arrow in cell ( i , j ) (i, j) (i,j) points leftwards.
  • If s i , j = ’r’ s_{i, j} = \text{'r'} si,j=’r’, the arrow in cell ( i , j ) (i, j) (i,j) points rightwards.
    For the following n n n lines, the i i i-th line contains m m m integers a i , 1 , a i , 2 , … , a i , m a_{i, 1}, a_{i, 2}, \dots, a_{i, m} ai,1,ai,2,,ai,m ( 1 ≤ a i , j ≤ max ⁡ ( n , m ) 1 \le a_{i, j} \le \max(n, m) 1ai,jmax(n,m)), where a i , j a_{i, j} ai,j indicates the integer in cell ( i , j ) (i, j) (i,j).

It’s guaranteed that the sum of n × m n \times m n×m of all test cases does not exceed 1 0 6 10^6 106.

Output

For each test case output one line. If BaoBao can find a proper initial cell, print “Yes” (without quotes), otherwise print “No” (without quotes).

Sample Input

2
2 3
rdd
url
2 1 1
1 1 2
2 2
rr
rr
1 1
1 1

Sample Output

Yes
No

题意

一个n*m的图,每个点有一个数字a[i][j],还有一个字母,为’u’,‘d’,‘l’,'r’中的一个,分别代表向上、下、左、右移动a[i][j]步的位置,是否存在走过所有点的方案。

题解:

一:欧拉路径,本身是哈密顿路径,但本题图特殊,可以作为有向图欧拉路径的判定:满足有向图基图连通,最多只有一对点满足,一点出度比入度大1,另一点入度比出度大1,其余点入度等于出度。
二:动态规划。 d p [ i ] [ j ] dp[i][j] dp[i][j]代表从i,j出发,最能到达的点数,然后DFS,维护栈,将点放入栈中,直到再次访问到栈内的点, 或者该点之前已更新、该点越界。再更改栈中点的信息即可。

欧拉路径:

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<map>
#include<vector>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 100010;
const int mod = 1000000000;
int in[maxn], out[maxn], a[maxn];
vector<char> g[maxn];
int Find(int x);

int main()
{
    int t, n, m, i, j, k, num, sig1, sig2;
    char ch;
    scanf("%d", &t);
    while(t--)
    {
        num = sig1 = sig2 = 0;
        scanf("%d %d", &n, &m);
        for(i=0;i<=n*m;i++){
            g[i].clear();
            a[i] = i;
            in[i] = out[i] = 0;
        }
        for(i=0;i<n;i++)
            for(j=0;j<m;j++){
                scanf(" %c", &ch);
                g[i].push_back(ch);
            }
        for(i=0;i<n;i++)
            for(j=0;j<m;j++){
                scanf("%d", &k);
                int nx, ny;
                if(g[i][j] == 'r')nx = i, ny = j+k;
                else if(g[i][j] == 'l')nx = i, ny = j-k;
                else if(g[i][j] == 'u')nx = i-k, ny = j;
                else nx = i+k, ny = j;
                if(nx>=0 && nx<n && ny>=0 && ny<m)
                {
                    int id1 = i*m+j, id2 = nx*m+ny, x=Find(id1), y = Find(id2);
                    in[id2]++, out[id1]++;
                    if(x != y){
                        a[x] = y;
                        num++;
                    }
                }
            }
        for(i=0;i<n*m;i++)
            if(in[i]>out[i])sig1 += in[i]-out[i];
            else sig2 += out[i]-in[i];
        if(num == n*m-1 && sig1<=1 && sig2<=1)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

int Find(int x)
{
    return a[x]=x==a[x]?x:Find(a[x]);
}

动态规划:

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<map>
#include<vector>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 100010;
const int mod = 1000000000;
int top, n, m;
P st[maxn];
vector<int> a[maxn], vis[maxn], dp[maxn], in[maxn];
vector<char> g[maxn];
void init(int n);
void dfs(int x, int y, int tim);

int main()
{
    int t, i, j, k;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &m);
        init(n);
        char ch;
        for(i=0;i<n;i++)
            for(j=0;j<m;j++){
                scanf(" %c", &ch);
                g[i].push_back(ch);
                dp[i].push_back(-1);
                vis[i].push_back(0);
                in[i].push_back(0);
            }
        for(i=0;i<n;i++)
            for(j=0;j<m;j++){
                scanf("%d", &k);
                a[i].push_back(k);
            }
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                if(vis[i][j] == 0)
                {
                    in[i][j] = 1;
                    top = 0;
                    st[++top] = P(i, j);
                    dfs(i, j, 1);
                }
        int mx = 0;
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                mx = max(mx, dp[i][j]);
        if(mx == n*m)printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}

void init(int n)
{
    for(int i=0;i<n;i++)
    {
        g[i].clear();
        a[i].clear();
        dp[i].clear();
        vis[i].clear();
        in[i].clear();
    }
}


void dfs(int x, int y, int tim)
{
    in[x][y] = 1;
    vis[x][y] = tim;
    int nx, ny, sig, up;
    if(g[x][y] == 'r')nx = x, ny = y+a[x][y];
    else if(g[x][y] == 'l')nx = x, ny = y-a[x][y];
    else if(g[x][y] == 'u')nx = x-a[x][y], ny = y;
    else nx = x+a[x][y], ny = y;
    if(nx>=0 && nx<n && ny>=0 && ny<m){
        if(vis[nx][ny] && in[nx][ny])sig = 1, up = tim+1-vis[nx][ny];
        else if(vis[nx][ny])sig = 0, up = dp[nx][ny]+1;
        else{
            st[++top] = P(nx,ny);
            dfs(nx, ny, tim+1);
        }
    }else sig = 0, up = 1;
    if(sig){
        while(top){
            dp[st[top].first][st[top].second] = up;
            in[st[top].first][st[top].second] = 0;
            top--;
           if(st[top+1].first == nx && st[top+1].second == ny)break;
        }
        up++;
        while(top){
            dp[st[top].first][st[top].second] = up;
            in[st[top].first][st[top].second] = 0;
            up++;
            top--;
        }
    }else{
        while(top){
            dp[st[top].first][st[top].second] = up;
            in[st[top].first][st[top].second] = 0;
            up++;
            top--;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这道题是一个典型的搜索问题,可以使用深度优先搜索(DFS)或广度优先搜索(BFS)来解决。以下是使用DFS的代码实现: ```c++ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 20; const int MAXM = 20; int n, m, sx, sy, ex, ey; char maze[MAXN][MAXM]; // 迷宫 int vis[MAXN][MAXM]; // 标记数组 int dx[] = {0, 0, 1, -1}; // 方向数组 int dy[] = {1, -1, 0, 0}; void dfs(int x, int y) { if (x == ex && y == ey) { // 到达终点 printf("(%d,%d)", x, y); return; } for (int i = 0; i < 4; i++) { // 依次尝试四个方向 int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] != '#' && !vis[nx][ny]) { vis[nx][ny] = 1; // 标记已访问 printf("(%d,%d)->", x, y); dfs(nx, ny); return; } } } int main() { while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { scanf("%s", maze[i]); for (int j = 0; j < m; j++) { if (maze[i][j] == 'S') { sx = i; sy = j; } else if (maze[i][j] == 'T') { ex = i; ey = j; } } } vis[sx][sy] = 1; dfs(sx, sy); printf("\n"); } return 0; } ``` 代码实现中,使用了一个标记数组 `vis` 来标记每个位置是否已经被访问过,避免走重复的路线。使用DFS的时候,每次从当前位置依次尝试四个方向,如果某个方向可以走,则标记该位置已经被访问过,并输出当前位置的坐标,然后递归进入下一个位置。如果当前位置是终点,则直接输出并返回。 在输出路径的时候,由于是递归调用,所以输出的路径是反向的,需要将其反转过来,即从终点往起点遍历输出。 需要注意的是,题目中要求输出的路径是 `(x1,y1)->(x2,y2)->...->(xn,yn)` 的形式,每个坐标之间用 `->` 连接。所以在输出的时候需要特别处理第一个坐标和最后一个坐标的格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值