123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #include <iostream>
- #include <list>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- class World
- {
- private:
-
- unsigned int l;
-
- unsigned int h;
-
- const unsigned int size;
-
- int* board;
- public:
-
- World(unsigned int l_, unsigned int h_, double p)
- :l(l_), h(h_), size(l_ * h_)
- {
- board = new int[size]();
-
- for (unsigned int i = 0; i < h; i++)
- {
- board[i * l] = 1;
- board[i * l + l - 1] = 1;
- }
-
- for (unsigned int j = 0; j < l; j++)
- {
- board[j] = 1;
- board[(h - 1) * l + j] = 1;
- }
- for (unsigned int i = 0; i < h; i++)
- {
- for (unsigned int j = 0; j < l; j++)
- {
-
-
- if ((double) rand() / RAND_MAX < p && !(i == 1 && j == 1) && !(i == h - 2 && j == l - 2))
- {
- board[i * l + j] = 1;
- }
- }
- }
- }
-
- void display()
- {
- for (unsigned int i = 0; i < h; i++)
- {
- for (unsigned int j = 0; j < l; j++)
- {
- switch (board[i * l + j])
- {
- case 0:
- cout << " ";
- break;
- case 1:
- cout << "#";
- break;
- }
- }
- cout << endl;
- }
- }
-
-
-
-
- unsigned int successors(unsigned int i, unsigned int r[4])
- {
- unsigned int n = 0;
- if (i >= 0 && i < size && board[i] != 1)
- {
-
-
- const unsigned int moves[] = { i - 1, i + 1, i - l, i + l};
- for (unsigned int k = 0; k < 4; k++)
- {
- if (board[moves[k]] != 1)
- {
- r[n] = moves[k];
- n++;
- }
- }
- }
- return n;
- }
-
-
-
-
- bool dfs(unsigned int s0, unsigned int t, list<unsigned int>& path)
- {
- bool r = false;
- int current = s0;
- bool explored[size];
- for (unsigned int k(0); k < size; k ++)
- explored[k] = false;
- explored[s0] = true;
- return r;
- }
- };
- int main()
- {
-
- srand(time(0));
-
- World w(20, 10, 0.2);
-
- w.display();
-
- unsigned int succs[4];
- unsigned int n = w.successors(21, succs);
- for (unsigned int k = 0; k < n; k++)
- {
- cout << succs[k] << " ";
- }
- cout << endl;
- return 0;
- }
|