Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save znxkznxk1030/98d024c511c12a2df0f7cd04a87720f0 to your computer and use it in GitHub Desktop.
Save znxkznxk1030/98d024c511c12a2df0f7cd04a87720f0 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <iostream>
#include <stack>
#include <cstring>
#include <queue>
#define ll long long int
#define MAX(a,b) (a)>(b)?(a):(b)
#define MIN(a,b) (a)>(b)?(b):(a)
#define pii pair<int , int>
#define turn(a) (a) < 0? (4 - a) : (a)
#define y second
#define x first
using namespace std;
int n, m;
int cr, cc, cd;
pii dir[4] = { {0, -1} , {1, 0}, {0, 1}, {-1, 0} };
struct robot {
int row, col, dir;
robot(int a, int b, int c) : row(a), col(b), dir(c) {}
robot(pii a, int c) : row(a.y), col(a.x), dir(c) {}
};
int matrix[61][61];
int cnt = 0;
void input() {
scanf("%d %d", &n, &m);
scanf("%d %d %d", &cr, &cc, &cd);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &matrix[i][j]);
}
}
}
int process(int r, int c, int d) {
int cnt = 0;
while (1)
{
if (matrix[r][c] == 0) //step 1
{
cnt++;
matrix[r][c] = 2;
}
bool wall = true;
for (int i = 1; i <= 4; i++) //step 2(1-2)
{
int nd = (d - i) < 0 ? (d - i + 4): (d - i);
int nr = r + dir[nd].y, nc = c + dir[nd].x;
if (nr >= 0 && nc >= 0 && nr < n && nc < m
&& matrix[nr][nc] == 0) {
r = nr;
c = nc;
d = nd;
wall = false;
break;
}
}
if (wall)
{
int nd = (d + 2) % 4;
int nr = r + dir[nd].y, nc = c + dir[nd].x;
if (matrix[nr][nc] == 1) //step 2(4) terminal condition
{
return cnt;
}
else { //step 2(3)
c = nc;
r = nr;
}
}
}
}
int main()
{
input();
printf("%d\n", process(cr, cc, cd));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment