More actions
imported>Unknown No edit summary |
(Repair MoniWiki formatting after migration) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 8: | Line 8: | ||
cout << "원하는 칸의 크기를 입력하세요 : "; | cout << "원하는 칸의 크기를 입력하세요 : "; | ||
cin >> num; | cin >> num; | ||
int square | int square[arsize][arsize] ={0,}; | ||
cout << "바퀴벌레의 처음 좌표를 입력하세요 (x,y): "; | cout << "바퀴벌레의 처음 좌표를 입력하세요 (x,y): "; | ||
cin >> x; | cin >> x; | ||
cin >> y; | cin >> y; | ||
square | square[x][y]=1; | ||
srand(0); | srand(0); | ||
while(end < num*num) | while(end < num*num) | ||
| Line 59: | Line 59: | ||
break; | break; | ||
} | } | ||
if (square | if (square[x][y]==0) | ||
end++; | end++; | ||
square | square[x][y]++; | ||
total++; | total++; | ||
| Line 69: | Line 69: | ||
{ | { | ||
for (int j=0 ; j<num ; j++) | for (int j=0 ; j<num ; j++) | ||
cout << square | cout << square[i][j] << "\t"; | ||
cout << endl; | cout << endl; | ||
} | } | ||
| Line 89: | Line 89: | ||
{ | { | ||
for (int j = 0; j < MAX_BOARD ; j++) | for (int j = 0; j < MAX_BOARD ; j++) | ||
board | board[i][j]=0; | ||
} | } | ||
EndCount=0; | EndCount=0; | ||
| Line 99: | Line 99: | ||
{ | { | ||
for (int j = 0; j < MAX_BOARD ; j++) | for (int j = 0; j < MAX_BOARD ; j++) | ||
cout << board | cout << board[i][j] << "\t" ; | ||
cout << endl; | cout << endl; | ||
} | } | ||
| Line 107: | Line 107: | ||
void Board::Make_Footprint(int a_x, int a_y) | void Board::Make_Footprint(int a_x, int a_y) | ||
{ | { | ||
board | board[a_x][a_y]++; | ||
if(board | if(board[a_x][a_y]==1) | ||
EndCount++; | EndCount++; | ||
} | } | ||
| Line 136: | Line 136: | ||
int way; | int way; | ||
way = rand()%8; | way = rand()%8; | ||
if (x+MOVE_X | if (x+MOVE_X[way]> -1 && x+MOVE_X[way]<5 && y+MOVE_Y[way] > -1 && y+MOVE_Y[way] < 5) | ||
{ | { | ||
x+=MOVE_X | x+=MOVE_X[way]; | ||
y+=MOVE_Y | y+=MOVE_Y[way]; | ||
} | } | ||
} | } | ||
== RandomWalk.cpp == | == [[RandomWalk]].cpp == | ||
#include <iostream> | #include <iostream> | ||
| Line 214: | Line 214: | ||
} | } | ||
Latest revision as of 00:34, 29 March 2026
#include <iostream>
using namespace std;
#include <ctime>
int const arsize = 11;
void main()
{
int num,x,y,cnt=0,total=0,end=1,temp;
cout << "원하는 칸의 크기를 입력하세요 : ";
cin >> num;
int square[arsize][arsize] ={0,};
cout << "바퀴벌레의 처음 좌표를 입력하세요 (x,y): ";
cin >> x;
cin >> y;
square[x][y]=1;
srand(0);
while(end < num*num)
{
temp = rand()%8;
switch (temp)
{
case 1 :
if(x-1>=0 && y-1>=0)
{
y--;
x--;
}
break;
case 2 :
if(x-1>=0)
x--;
break;
case 3 :
if(x-1>=0 && y+1<num)
{
x--;
y++;
}
break;
case 4 : if(y-1>=0)
y--;
break;
case 5 : if(y+1<num)
y++;
break;
case 6 : if(x+1<num && y-1>=0)
{
x++;
y--;
}
break;
case 7 : if(x+1<num)
x++;
break;
case 0 : if(x+1<num && y+1<num)
{
x++;
y++;
}
break;
}
if (square[x][y]==0)
end++;
square[x][y]++;
total++;
}
for (int i = 0 ; i<num ; i++)
{
for (int j=0 ; j<num ; j++)
cout << square[i][j] << "\t";
cout << endl;
}
cout << endl << "총 이동횟수는 " <<total<<endl;
}
최근에 C++ 로 짠것..
Board.cpp
#include <iostream>
using namespace std;
#include"Board.h"
#include"Bug.h"
Board::Board()
{
for (int i = 0; i < MAX_BOARD ; i++)
{
for (int j = 0; j < MAX_BOARD ; j++)
board[i][j]=0;
}
EndCount=0;
}
void Board::Show_Board()
{
for (int i = 0; i < MAX_BOARD ; i++)
{
for (int j = 0; j < MAX_BOARD ; j++)
cout << board[i][j] << "\t" ;
cout << endl;
}
cout << endl;
}
void Board::Make_Footprint(int a_x, int a_y)
{
board[a_x][a_y]++;
if(board[a_x][a_y]==1)
EndCount++;
}
bool Board::IsNotEnd()
{
if (EndCount < 25)
return true;
else
return false;
}
Bug.cpp
#include <iostream>
using namespace std;
#include"Bug.h"
#include <ctime>
Bug::Bug()
{
x=0; y=0;
}
void Bug::move()
{
int way;
way = rand()%8;
if (x+MOVE_X[way]> -1 && x+MOVE_X[way]<5 && y+MOVE_Y[way] > -1 && y+MOVE_Y[way] < 5)
{
x+=MOVE_X[way];
y+=MOVE_Y[way];
}
}
RandomWalk.cpp
#include <iostream>
using namespace std;
#include "Board.h"
#include "Bug.h"
#include <ctime>
void main()
{
srand((unsigned)time(NULL));
Bug bug1;
Board board;
board.Make_Footprint(bug1.return_x(), bug1.return_y());
board.Show_Board();
do{
bug1.move();
board.Make_Footprint(bug1.return_x(), bug1.return_y());
board.Show_Board();
}while(board.IsNotEnd());
}
Board.h
#include <iostream>
using namespace std;
#include "Board.h"
#include "Bug.h"
#include <ctime>
void main()
{
srand((unsigned)time(NULL));
Bug bug1;
Board board;
board.Make_Footprint(bug1.return_x(), bug1.return_y());
board.Show_Board();
do{
bug1.move();
board.Make_Footprint(bug1.return_x(), bug1.return_y());
board.Show_Board();
}while(board.IsNotEnd());
}
Bug.h
#include <iostream>
using namespace std;
#include "Board.h"
#include "Bug.h"
#include <ctime>
void main()
{
srand((unsigned)time(NULL));
Bug bug1;
Board board;
board.Make_Footprint(bug1.return_x(), bug1.return_y());
board.Show_Board();
do{
bug1.move();
board.Make_Footprint(bug1.return_x(), bug1.return_y());
board.Show_Board();
}while(board.IsNotEnd());
}