## 9/06/2011

### Conduct an investigation about length of snake. And write sequence number on it's body.

A huge snake has appeared in our village.
If we cannot kill the snake, it is clear that village is damaged by snake.
Many people are under investigation to kill the snake.
We also receive the mission that we write sequence number on it's body.
The number is written as sequence from 1.
Let's solve the problem hurry and save the our village

-Input is txt file.
The name is "input.txt"
Input file consist of like below.
.size of width, size of height
.map information

-output
You should save as "output.txt".
The file include sequence number about snake.

--------------------------------------------------------

μ΄λ€ λ§μμ κ±°λν λ±μ΄ λνλ¬λ€.
λΉ¨λ¦¬ λ±μ μ£½μ΄μ§ μμΌλ©΄ λ§μμ ν° νΌν΄κ° μ¨λ€.
μ¬λλ€μ μ΄ λ±μ μ‘κΈ° μν μ¬λ¬κ°μ§ λ°©λ²μ μ‘°μ¬ μ€μ΄λ€.
μ°λ¦¬λ λ§μλ‘ λΆν° λ± λͺΈμ λ§λλ§λ€ μ«μλ₯Ό μ°λΌλ μλ¬΄λ₯Ό λ°μλ€.
μ«μλ 1μ μμμΌλ‘ μμ°¨μ μΌλ‘ κΈ°λ‘ν΄μΌ νλ€.
μ~ λΉ¨λ¦¬ λ±μ λ§λμ μ«μλ₯Ό μ°κ³  λ§μμ κ΅¬νμ~!

μλ ₯
λ§μ κ°λ‘ν¬κΈ°, λ§μ μΈλ‘ν¬κΈ°

μΆλ ₯
λ±μ λ§λμ μμ°¨μ μ μ«μλ‘ λμ²΄

ex1)
- input.txt -
5 5
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
0 1 1 1 0
0 0 0 0 0

-output.txt-
0 0 0 0 0
0 1 2 3 0
0 0 0 4 0
0 7 6 5 0
0 0 0 0 0

ex2)
-input.txt-
10 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0

-output.txt-
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 2 0 0 0 0 0 0 0
0 0 3 4 0 0 0 0 0 0
0 0 0 5 0 0 0 0 0 0
0 0 0 6 7 0 0 0 0 0
0 0 0 0 8 9 10 11 0 0
0 0 0 0 0 0 0 0 12 0
0 0 0 0 0 0 0 0 13 0
0 0 0 0 0 0 0 0 0 0

%λ±μ λ¨Έλ¦¬λ ν­μ κΌ¬λ¦¬λ λͺΈμ μμΉλ³΄λ€ μΌμͺ½ κ·Έλ¦¬κ³  μμͺ½μ μ‘΄μ¬νλ€.
%input.txt νμΌμ μ½μ΄μ output.txt νμΌλ‘ μΆλ ₯νλ κ²μ λͺ©νλ‘ νλ€.
%λ±μ νμ 90λλ‘ κΊ½μ¬μλ€. μ¦, λκ°μ μΌλ‘ μ°κ²°λμ΄ μμ§ μλ€!

*Sample code for read file and Dynamic allocation.

```FILE * fp;
fp = fopen("input.txt","r");

int w,h;
fscanf(fp,"%d",&w);
fscanf(fp,"%d",&h);

int **map;
map = new int*[h];
for(int i=0; i<h; ++i)
map[i] = new int[w];

for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
fscanf(fp,"%d",&(map[i][j]) );

//Confirmation
for(int i=0; i<h; ++i)
{
for(int j=0; j<w; ++j)
printf("%d ", map[i][j] );
printf("\n");
}

//memory release
for(int i=0; i<h; ++i)
delete map[i];
delete[] map;
//File close
fclose(fp);

///
```

1. Anonymous13/9/11 18:47

μ μ΄λ² κ³Όμ λ νλ€μμ΄μ :)

JeKang's code :

#include

void findhead(int **map, int* hw, int* hh)
{
for(int i=0; i>=0; ++i){
for(int j=0; j<=i; ++j){
if(map[i][j]==1){
*hw=i;
*hh=j;
break;
}
}
if(map[*hw][*hh]==1) break;
}
}

void makebody(int **map, int* hw, int* hh)
{
//Make snake body
int Num=1, w=0, h=0;
w=*hw;
h=*hh;
while(1){
if(map[w][h]==Num){
for(int i=-1; i<=1; i+=2){
if(map[w+i][h]==1 && (w+i!=*hw || h!=*hh)){
map[w+i][h]=Num+1;
w+=i;
Num++;
break;
}
else if(map[w][h+i]==1 && (h+i!=*hh || w!=*hw)){
map[w][h+i]=Num+1;
h+=i;
Num++;
break;
}
else if(i==1) Num=0;
}
if(Num==0) break;
}
}
}

void main()
{
FILE * fp;
fopen_s(&fp, "input.txt","r");

int w=0,h=0,hw=0,hh=0;
fscanf_s(fp,"%d",&w);
fscanf_s(fp,"%d",&h);

int **map;
map = new int*[h];
for(int i=0; i<h; ++i) map[i] = new int[w];

for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j) fscanf_s(fp,"%d", &(map[i][j]));

//File close
fclose(fp);

//Call body function
makebody(map, &hw, &hh);

//Confirmation
for(int i=0; i<h; ++i)
{
for(int j=0; j<w; ++j) printf("%d ", map[i][j]);
printf("\n");
}

FILE * fo;
fopen_s(&fo, "output.txt","w");

//Write snake map information
for(int i=0; i<h; ++i){
for(int j=0; j<w; ++j) fprintf(fo,"%d ", map[i][j]);
fprintf(fo,"\n");
}

//File close
fclose(fo);

//memory release
for(int i=0; i<h; ++i)
delete map[i];
delete[] map;
}