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"); //Read Width, Height int w,h; fscanf(fp,"%d",&w); fscanf(fp,"%d",&h); //Dynamic allocation about w,h sizes int **map; map = new int*[h]; for(int i=0; i<h; ++i) map[i] = new int[w]; //Read map information 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); ///
와 이번 과제는 힘들었어요 :)
ReplyDeleteJeKang's code :
#include
void findhead(int **map, int* hw, int* hh)
{
//Find snake head
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");
//Read Width, Height
int w=0,h=0,hw=0,hh=0;
fscanf_s(fp,"%d",&w);
fscanf_s(fp,"%d",&h);
//Dynamic allocation about w,h sizes
int **map;
map = new int*[h];
for(int i=0; i<h; ++i) map[i] = new int[w];
//Read map information
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 head function
findhead(map, &hw, &hh);
//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;
}
https://docs.google.com/document/d/1E42IQEAa0yeEK8q84cOYD-HJP2S-vTziqNyoOa24ZuE/edit?hl=ko
ReplyDelete위 주소 소스 참고해..댓글로 소스가 안올라가네..
일단 Findhead 함수가 잘못되었고
ReplyDeleteBody함수는 다시 보고 말해줄께.
난 재귀호출로 만들었는데 반복문으로도 만들어서 올려줄께..
수고했다.