Grid with Arrows
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) (i−ai,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,j−ai,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 1≤n×m≤105), 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) 1≤ai,j≤max(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--;
}
}
}