QM22B Editorial

PROBLEM LINK:

Practice
Contest

Author: Chandan Boruah
Tester: Chandan Boruah
Editorialist: Chandan Boruah

DIFFICULTY:

EASY

PREREQUISITES:

Brute Force

PROBLEM:

Given a 2D grid which moves on a keypress on keyboard, a before and after state of the grid is given. Print which key has been pressed, print -1 if it can’t be determined.

QUICK EXPLANATION:

Create four new grids from the original 2D grid, and compare it to the new grid, if exactly one grid is matching print the corresponding output, else print -1.

EXPLANATION:

Create four new grids from the original 2D grid, and compare it to the new grid, if exactly one grid is matching print the corresponding output, else print -1. Since, if more than one grid comparison tallys then it means that it can’t be determined.

SOLUTIONS:

chandubaba's Solution
using System;
using System.Collections.Generic;
class some
{
	public static void Main()
	{
		string[]ss=Console.ReadLine().Split();
		int n=int.Parse(ss[0]);
		int m=int.Parse(ss[1]);
		string[]ret=new string[n];
		for(int i=0;i<n;i++)
		{
			ret[i]=Console.ReadLine();
		}
		string[]ret2=new string[n];
		for(int i=0;i<n;i++)
		{
			ret2[i]=Console.ReadLine();
		}

		string[]u=up(ret);
		string[]d=down(ret);

		string[]l=left(ret);
		string[]r=right(ret);
		bool uu=yes(u,ret2);
		bool dd=yes(d,ret2);
		bool ll=yes(l,ret2);
		bool rr=yes(r,ret2);
		
		int c=0;
		if(uu)c++;if(dd)c++;if(ll)c++;if(rr)c++;
			
		if(c==0 || c>1)Console.WriteLine(-1);
		else
		{
			if(uu)Console.WriteLine("U");
			else if(dd)Console.WriteLine("D");
			else if(ll)Console.WriteLine("L");
			else Console.WriteLine("R");
		}
	}
	public static bool yes(string[]a,string []b)
	{
		for(int i=0;i<a.Length;i++)
		{
			for(int j=0;j<b[0].Length;j++)
			{
				if(a[i][j]!=b[i][j])return false;
			}
			
		}return true;
	}
	public static string [] up(string []ret)
	{
		string v="";
		for(int i=0;i<ret[0].Length;i++)v+=".";
		string[]vv=new string[ret.Length];
		for(int i=0;i<ret.Length-1;i++)
		{
			vv[i]=ret[i+1];
		}
		vv[ret.Length-1]=v;
		return vv;
	}
	public static string [] down(string []ret)
	{
		string v="";
		for(int i=0;i<ret[0].Length;i++)v+=".";string[]vv=new string[ret.Length];
		for(int i=1;i<ret.Length;i++)
		{
			vv[i]=ret[i-1];
		}
		vv[0]=v;
		return vv;
	}
	public static string [] left(string []ret)
	{
		string[]vv=new string[ret.Length];
		for(int i=0;i<ret.Length;i++)
		{
			if(ret[i].Length==1){vv[i]=".";continue;}
			vv[i]=ret[i].Substring(1);
			vv[i]+=".";
		}
		return vv;
	}
	public static string [] right(string []ret)
	{
		string[]vv=new string[ret.Length];
		for(int i=0;i<ret.Length;i++)
		{
			if(ret[i].Length==1){vv[i]=".";continue;}
			vv[i]=ret[i].Substring(0,ret[i].Length-1);
			vv[i]="."+ret[i];
		}
		return vv;
	}
}
1 Like

https://www.codechef.com/viewsolution/29718946
Can you please tell the test case for which my code is giving WA ?

1 Like

hi! cubefreak777 … I was intending to say that the entire grid moves, not any single ‘#’. I feel you checked for a single one.

1 Like

yes ,you are right but we can’t we analyse the motion of the grid by just tracking one of the ‘#’ as it is a part of grid too ?

1 Like

noooo!! example:

Initial

.......
.....#..
....#...

Final

.....#..
.......
....#...

Only one # moved, however the entire grid didn’t move!

1 Like

Oh ! Thanks buddy for such a prompt response :+1:

1 Like

welcome!