Created
April 6, 2016 03:16
-
-
Save nomarlo/8abed6972f375eeb0b8161fd52daf470 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Hay que tener cuidado con el formato de salida: | |
"...each value right justified in a field of width 3" (cada valor justificado en un campo de tamaño 3) | |
Y con la idea de que si se pierde la pared de la derecha y encontramos con pared de frente, | |
tiene prioridad encontrar pared girando a la derecha, antes que hacer el giro a a la izquierda | |
**/ | |
#include <iostream> | |
#include <cmath> | |
#include <algorithm> | |
#include <queue> | |
#include <stack> | |
#include <sstream> | |
#include <map> | |
#include <set> | |
#include <queue> | |
#include <cstdio> | |
using namespace std; | |
struct compare | |
{ | |
bool operator()(const int& l, const int& r) | |
{ | |
return l > r; | |
} | |
}; | |
int T[10005][10005]; | |
int R[8]; | |
int w,h; | |
bool esMuro(int x, int y, int sentido){ | |
switch(sentido){ | |
case 0: | |
return T[y-1][x]==-2; | |
break; | |
case 1: | |
return T[y][x+1]==-2; | |
break; | |
case 2: | |
return T[y+1][x]==-2; | |
break; | |
case 3: | |
return T[y][x-1]==-2; | |
break; | |
} | |
} | |
bool muroDer(int x, int y, int sentido){//verificar si hay muro a la derecha | |
switch(sentido){ | |
case 0: | |
return T[y][x+1]==-2; | |
break; | |
case 1: | |
return T[y+1][x]==-2; | |
break; | |
case 2: | |
return T[y][x-1]==-2; | |
break; | |
case 3: | |
return T[y-1][x]==-2; | |
break; | |
} | |
} | |
int main(){ | |
scanf("%d %d",&h,&w); | |
while(w){ | |
for(int i=0;i<h+5;i++){ | |
for(int e=0;e<w+5;e++){ | |
T[i][e]=-2; | |
} | |
} | |
//Empezamos desde 1,1 para evitar salirnos, es decir todo de 0,1 0,2 ...n,0 es pared, as� como 1,0, 2,0...n,0 | |
for(int i=1;i<=h;i++){ | |
for(int e=1;e<=w;e++){ | |
scanf("%1d",&T[i][e]); | |
T[i][e]=T[i][e]?-2:0;//si es 1 lo mandamos a -2 (muro) | |
} | |
} | |
int x,y; | |
x=1; | |
y=h; | |
int sentido=1;//0:norte, 1:este 2:sur 3:oeste | |
while(1){ | |
//cout<<x<<":"<<y<<endl; | |
if(!muroDer(x,y,sentido)){ | |
sentido=(sentido+1)%4; | |
} | |
while(esMuro(x,y,sentido)){ | |
sentido=((sentido-1)+4)%4; | |
} | |
//avanzar | |
switch(sentido){ | |
case 0: | |
T[y][x]+=1; | |
y--; | |
break; | |
case 1: | |
T[y][x]+=1; | |
x++; | |
break; | |
case 2: | |
T[y][x]+=1; | |
y++; | |
break; | |
case 3: | |
T[y][x]+=1; | |
x--; | |
break; | |
} | |
if(x==1 && y==h){//regresamos al punto de origen | |
break; | |
} | |
} | |
for(int i=0;i<5;i++) | |
R[i]=0; | |
for(int i=1;i<=h;i++){ | |
for(int e=1;e<=w;e++){ | |
if(T[i][e]>=0 && T[i][e]<=4) | |
R[T[i][e]]++; | |
} | |
} | |
printf("%3d%3d%3d%3d%3d\n",R[0],R[1],R[2],R[3],R[4]); | |
scanf("%d %d",&h,&w); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment