Saturday, September 17, 2016

UVA 118 - Mutant Flatworld Explorers

#include<bits/stdc++.h>
using namespace std;
bool destination, flg[100][100];
int nx , ny, row , col;
char orientation;
void process(char x)
{
if(orientation == 'S' && x == 'R')orientation = 'W';
else if(orientation == 'S' && x == 'L')orientation = 'E';
else if(orientation == 'N' && x == 'R')orientation = 'E';
else if(orientation == 'N' && x == 'L')orientation = 'W';
else if(orientation == 'E' && x == 'R')orientation = 'S';
else if(orientation == 'E' && x == 'L')orientation = 'N';
else if(orientation == 'W' && x == 'R')orientation = 'N';
else if(orientation == 'W' && x == 'L')orientation = 'S';
if(x == 'F'){
switch(orientation){
case 'N':
if(row == ny && flg[nx][ny])break;
else if(row == ny && !flg[nx][ny]){
flg[nx][ny] = true;
cout<<nx<<' '<<ny<<' '<<orientation<<" LOST\n";
destination = true;
break;
}
ny++;
break;
case 'S':
if(ny == 0 && flg[nx][ny])break;
else if(ny == 0 && !flg[nx][ny]){
flg[nx][ny] = true;
cout<<nx<<' '<<ny<<' '<<orientation<<" LOST\n";
destination = true;
break;
}
ny--;
break;
case 'E':
if(col == nx && flg[nx][ny])break;
else if(col == nx && !flg[nx][ny]){
flg[nx][ny] = true;
cout<<nx<<' '<<ny<<' '<<orientation<<" LOST\n";
destination = true;
break;
}
nx++;
break;
case 'W':
if(nx == 0 && flg[nx][ny])break;
else if(nx == 0  && !flg[nx][ny]){
flg[nx][ny] = true;
cout<<nx<<' '<<ny<<' '<<orientation<<" LOST\n";
destination = true;
break;
}
nx--;

}
}
}

int main()
{
memset(flg,false,sizeof flg);
cin>>col>>row;
while(cin>>nx>>ny>>orientation){
char com[100];
cin>>com;
destination = false;
for(int i = 0; com[i] && !destination; i++){
process(com[i]);
}
if(!destination)cout<<nx<<' '<<ny<<' '<<orientation<<'\n';
}

return 0;
}

Wednesday, September 7, 2016

UVA 572 - Oil Deposits

#include<bits/stdc++.h>
using namespace std;
int m , n , cnt;
vector<string>v;
string s;
bool vis[100][100];
int nx[8] = {1,1,1,-1,-1,-1,0,0};
int ny[8] = {1,0,-1,1,0,-1,1,-1};
void dfs(int start , int end)
{
vis[start][end] = 1;
int row , col;
for(int i=0; i<8; i++){
row = start + nx[i];
col = end + ny[i];
if(row >=0 && row<m && col>=0 && col<n && !vis[row][col]){
vis[row][col] = 1;
if(v[row][col] == '@')dfs(row,col);
}
}
}

int main()
{
while(cin>>m>>n && m){
v.clear();
for(int i=0; i<m; i++){
cin>>s;
v.push_back(s);
}
        cnt = 0;
        memset(vis,0,sizeof(vis));
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(!vis[i][j]){
vis[i][j] = true;
if(v[i][j] == '@'){cnt++; dfs(i,j);}
}
}
}
cout<<cnt<<'\n';
}

return 0;
}