RUNNING LAVA(DFS)
作者:互联网
#include <iostream> #include <ctime> #include <cstdlib> #include <algorithm> #include <string> #include <cstring> #include <conio.h> #include <windows.h> #define BLOCK "■" #define maxn 52 using namespace std; char map[maxn][maxn]; int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; enum color{ BLACK , DARKBLUE , GREEN , BLUE , RED , PURPLE , YELLOW , WHITE , GREY , LIGHTBLUE , LIGHTGREEN , GREENBLUE , LIGHTRED , LIGHTPURPLE , LIGHTYELLOW , LIGHTWHITE }; int clr_rand[9] = {RED , PURPLE , DARKBLUE , LIGHTRED , YELLOW , GREENBLUE , GREEN , BLUE , LIGHTPURPLE}; int _clr; void Handle(int , int); void print(int , int); void set_color(int ); int random(int ); void develop(char map[maxn][maxn]); bool check(int , int); int dfs(int , int); int main(){ srand((int)time(NULL)); cout << "PLEASE DISPLAY IN A FULL SCREEN\n (press the key to continue)" ; char c = getch(); for(int i = 0 ; i < maxn ; ++i){ for(int j = 0 ; j < maxn ; ++j){ if(i == 0 || j == 0 || i == maxn - 1 || j == maxn - 1){ map[i][j] = 's'; } else map[i][j] = ' '; } } int block_num = random(maxn * maxn / 2); while(block_num--){ int x = random(maxn - 2) + 1; int y = random(maxn - 2) + 1; map[x][y] = 'b'; } for(int i = 0 ; i < maxn ; ++i){ for(int j = 0 ; j < maxn ; ++j){ print(i , j); } cout << endl; } bool flag = true; develop(map); system("pause"); return 0; } void Handle(int screen_x , int screen_y){ HANDLE hOut; COORD pos = {short(screen_x * 2),short(screen_y)}; hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); } void print(int i , int j){ Handle(i , j); if(map[i][j] == ' ') cout << " "; else if(map[i][j] == 's') cout << BLOCK; else if(map[i][j] == 'l'){ set_color(clr_rand[_clr]); cout << BLOCK; set_color(WHITE); } else { // set_color(BLUE); cout << BLOCK; set_color(WHITE); } set_color(WHITE); } int random(int mod){ return rand() % mod ; } void set_color(int clr){ if(clr >= BLACK && clr <= LIGHTWHITE) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), clr); else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), WHITE); } bool check(int x , int y){ if(x >= 1 && x < maxn - 1 && y >= 1 && y < maxn - 1 && map[x][y] == ' ') return true; else return false; } int dfs(int x , int y){ if(check(x , y)){ map[x][y] = 'l'; print(x , y); Sleep(50); for(int i = 0 ; i < 4 ; ++i){ dfs(x + dir[i][0] , y + dir[i][1]); } } return 0; } void develop(char map[maxn][maxn]){ for(int i = 1 ; i < maxn - 1 ; ++i) for(int j = 1 ; j < maxn - 1 ; ++j) if(map[i][j] == ' ') _clr = (_clr + 1) % 9 , dfs(i , j); Handle(0 , maxn + 1); }
Display:
标签:map,int,void,LAVA,RUNNING,maxn,DFS,include,clr 来源: https://www.cnblogs.com/RadiumGalaxy/p/running_lava.html