Educational Codeforces Round 128 (Rated for Div. 2)

比赛链接

Educational Codeforces Round 128 (Rated for Div. 2)

B. Robots

There is a field divided into \(n\) rows and \(m\) columns. Some cells are empty (denoted as \(\mathrm{E}\) ), other cells contain robots (denoted as \(\mathrm{R}\) ).
You can send a command to all robots at the same time. The command can be of one of the four types:

  • move up;
  • move right;
  • move down;
  • move left.

When you send a command, all robots at the same time attempt to take one step in the direction you picked. If a robot tries to move outside the field, it explodes; otherwise, every robot moves to an adjacent cell in the chosen direction.

You can send as many commands as you want (possibly, zero), in any order. Your goal is to make at least one robot reach the upper left corner of the field. Can you do this without forcing any of the robots to explode?

Input

The first line contains one integer \(t(1 \leq t \leq 5000)\) —— the number of test cases.
Each test case starts with a line containing two integers \(n\) and \(m(1 \leq n, m \leq 5)\) —— the number of rows and the number of columns, respectively. Then \(n\) lines follow; each of them contains a string of \(m\) characters. Each character is either \(E\) (empty cell } or \(R\) (robot).
Additional constraint on the input: in each test case, there is at least one robot on the field.

Output

If it is possible to make at least one robot reach the upper left corner of the field so that no robot explodes, print YES. Otherwise, print NO.

解题思路

思维

要求左上角出现机器人,则尽量将所有的机器人向左上角移动即可,最后判断左上角是否有机器人,如果向其他方向移动显然只会离答案越来越远

  • 时间复杂度:\(O(tnm)\)

暴力dfs

直接枚举所有指令,注意不能形成同一局面,否则会无限循环下去,枚举所有局面的复杂度为 \(O(nm\times (n+m))\),另外判断是否处于同一局面用到的哈希表的常数过大,需要剪枝:只向左上两个方向移动
注意:这里拷贝恢复现场要用局部变量!

  • 时间复杂度:\(O(tnm\times (n+m))\)

代码

  • 思维
// Problem: B. Robots
// Contest: Codeforces - Educational Codeforces Round 128 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1680/problem/B
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

int t,n,m;
char g[7][7];
int main()
{
    for(cin>>t;t;t--)
    {
    	cin>>n>>m;
    	for(int i=1;i<=n;i++)cin>>(g[i]+1);
    	bool f=(g[1][1]=='R');
    	if(!f)
    	{
    		int t=6;
    		for(int i=1;i<=n;i++)
    		{
    			int tt=0;
    			for(int j=1;j<=m;j++)
    				if(g[i][j]=='R')
    				{
    					tt=j;
    					break;
    				}
    			if(tt)t=min(t,tt);
    		}
    		if(t!=6)
    		{
    			for(int i=1;i<=n;i++)
    				for(int j=1;j<=m;j++)
    					if(g[i][j]=='R')g[i][j]='E',g[i][j-t+1]='R';
    		}
    		t=0;
    		for(int i=1;i<=n;i++)
    			if(g[i][1]=='R')
    			{
    				t=i;
    				break;
    			}
    		if(t)
    		{
    			f=true;
    			for(int j=2;j<=m;j++)
    			{
    				int tt=0;
    				for(int i=1;i<=n;i++)
    					if(g[i][j]=='R')
    					{
    						tt=i;
    						break;
    					}
    				if(tt&&t>tt)
    				{
    					f=false;
    					break;
    				}
    			}
    		}
    	}
    	puts(f?"YES":"NO");
    }
    return 0;
}
  • 暴力dfs
// Problem: B. Robots
// Contest: Codeforces - Educational Codeforces Round 128 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1680/problem/B
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

int t,n,m;
bool g[6][6];
int dx[]={0,-1},dy[]={-1,0};
set<vector<int>> s;
bool ck(int d)
{
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(g[i][j])
			{
				int ni=i+dx[d],nj=j+dy[d];
				if(!(ni>=1&&ni<=n&&nj>=1&&nj<=m))return false;
			}
		}
	return true;
}
bool dfs()
{
	if(g[1][1])return true;
	for(int i=0;i<4;i++)
	{
		if(ck(i))
		{
			bool bg[6][6],bv[6][6];
			memset(bg,0,sizeof bg);
			memcpy(bv,g,sizeof g);
			vector<int> v;
			for(int j=1;j<=n;j++)
				for(int k=1;k<=m;k++)
				{
					if(g[j][k])
					{
						int ni=j+dx[i],nj=k+dy[i];
						bg[ni][nj]=1;
						v.pb((ni-1)*m+nj-1);
					}
				}
			if(s.count(v))continue;
			s.insert(v);
			memcpy(g,bg,sizeof bg);
			if(dfs())return true;
			auto t=s.find(v);
			s.erase(t);
			memcpy(g,bv,sizeof bv);
		}
	}
	return false;
}
int main()
{
	help;
    for(cin>>t;t;t--)
    {
    	s.clear();
    	cin>>n>>m;
    	int t=0;
    	vector<int> v;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++)
    		{
    			char c;
    			cin>>c;
    			g[i][j]=(c=='R');
    			if(g[i][j])v.pb((i-1)*m+j-1);
    		}
    	s.insert(v);
    	puts(dfs()?"YES":"NO");
    }
    return 0;
}
posted @ 2022-05-14 22:34  zyy2001  阅读(45)  评论(0编辑  收藏  举报