Forum

8x8x8 Vezir problem...
 
Notifications
Clear all

8x8x8 Vezir problemi  

  RSS

sonsuz
(@sonsuz)
Admin
Katılım : 4 sene önce
Gönderiler: 42
07/12/2019 11:05 am  
#include <iostream>
#include <cstdlib>
#include <vector>
#include <sstream>
#include <cctype>
#include <Windows.h>
#include <ctime>
#include <stdexcept>
#include <fstream>
using namespace std;

#define PRINT(vec) for(int i = 0; i < vec.size(); i++) cout << vec[i] << endl;

int board[8][8][8] = {0}, moves = 0;

template<typename T>
string toString(T arg)
{
	stringstream ss;
	ss << arg;
	return ss.str();
}

void Clear()
{
	for(int x = 0; x < 8; x++)
	{
		for(int y = 0; y < 8; y++)
		{
			for(int z = 0; z < 8; z++)
				board[x][y][z] = 0;
		}
	}
	moves = 0;
}

void ClearScreen() {
    COORD topLeft  = { 0, 0 };
    HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO screen;
    DWORD written;

    GetConsoleScreenBufferInfo(console, &screen);
    FillConsoleOutputCharacterA(
        console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
    );
    FillConsoleOutputAttribute(
        console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
        screen.dwSize.X * screen.dwSize.Y, topLeft, &written
    );
    SetConsoleCursorPosition(console, topLeft);
}

void print()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
	for(int x = 0; x < 8; x++)
	{
		for(int y = 0; y < 8; y++)
		{
			for(int z = 0; z < 8; z++)
			{
				if(board[x][y][z] == 2)
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
				if(board[x][y][z] == 1)
					SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
				printf("%d", board[x][y][z]);
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
				printf(" ");
			}
			printf("\n");
			
		}
		printf("\n");
	}
}

void write_to_file(string additional)
{
	ofstream yaz("maksimum.txt");
	for(int x = 0; x < 8; x++)
	{
		for(int y = 0; y < 8; y++)
		{
			for(int z = 0; z < 8; z++)
			{
				yaz << board[x][y][z];
			}
			yaz << endl;
			
		}
		yaz << endl;
	}
	yaz << endl << additional;
}

bool Place(int X, int Y, int Z)
{
	swap(Y, X);
	if(board[Z][X][Y] == 1 || board[Z][X][Y] == 2) return false;
	board[Z][X][Y] = 2;
	for(int x = 0; x < 8; x++)
	{
		if(x != Z && board[x][X][Y] != 2) board[x][X][Y] = 1;
		if(x != X && board[Z][x][Y] != 2) board[Z][x][Y] = 1;
		if(x != Y && board[Z][X][x] != 2) board[Z][X][x] = 1;
	}
	for(int i = X, j = Y; i < 8 && j < 8; i++, j++)
		if(i != X && j != Y && board[Z][i][j] != 2) board[Z][i][j] = 1;
	for(int i = X, j = Y; i >= 0 && j < 8; i--, j++)
		if(i != X && j != Y && board[Z][i][j] != 2) board[Z][i][j] = 1;
	for(int i = X, j = Y; i < 8 && j >= 0; i++, j--)
		if(i != X && j != Y && board[Z][i][j] != 2) board[Z][i][j] = 1;
	for(int i = X, j = Y; i >= 0 && j >= 0; i--, j--)
		if(i != X && j != Y && board[Z][i][j] != 2) board[Z][i][j] = 1;
	
	for(int i = X, j = Y, k = Z; i < 8 && j < 8 && k < 8; i++, j++, k++)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	for(int i = X, j = Y, k = Z; i >= 0 && j >= 0 && k < 8; i--, j--, k++)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	for(int i = X, j = Y, k = Z; i < 8 && j >= 0 && k < 8; i++, j--, k++)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	for(int i = X, j = Y, k = Z; i >= 0 && j < 8 && k < 8; i--, j++, k++)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	
	for(int i = X, j = Y, k = Z; i < 8 && j < 8 && k >= 0; i++, j++, k--)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	for(int i = X, j = Y, k = Z; i >= 0 && j >= 0 && k >= 0; i--, j--, k--)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	for(int i = X, j = Y, k = Z; i < 8 && j >= 0 && k >= 0; i++, j--, k--)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	for(int i = X, j = Y, k = Z; i >= 0 && j < 8 && k >= 0; i--, j++, k--)
		if(i != X && j != Y && k != Z && board[k][i][j] != 2) board[k][i][j] = 1;
	
	for(int x = X, k = Z; x < 8 && k < 8; x++, k++)
		if(x != X && board[k][x][Y] != 2) board[k][x][Y] = 1;	
	for(int x = Y, k = Z; x < 8 && k < 8; x++, k++)
		if(x != Y && board[k][X][x] != 2) board[k][X][x] = 1;
	
	for(int x = X, k = Z; x >= 0 && k < 8; x--, k++)
		if(x != X && board[k][x][Y] != 2) board[k][x][Y] = 1;
	for(int x = Y, k = Z; x >= 0 && k < 8; x--, k++)
		if(x != Y && board[k][X][x] != 2) board[k][X][x] = 1;
	
	for(int x = X, k = Z; x < 8 && k >= 0; x++, k--)
		if(x != X && board[k][x][Y] != 2) board[k][x][Y] = 1;
	for(int x = Y, k = Z; x < 8 && k >= 0; x++, k--)
		if(x != Y && board[k][X][x] != 2) board[k][X][x] = 1;
	
	for(int x = X, k = Z; x >= 0 && k >= 0; x--, k--)
		if(x != X && board[k][x][Y] != 2) board[k][x][Y] = 1;
	for(int x = Y, k = Z; x >= 0 && k >= 0; x--, k--)
		if(x != Y && board[k][X][x] != 2) board[k][X][x] = 1;
	moves ++;
	return true;
}

vector<int> randomVec()
{
	vector<int> d = {0,1,2,3};
	vector<int> d2;
	do
	{
		int r = rand()%d.size();
		d2.push_back(d[r]);d.erase(d.begin() + r);
	}while(d.size() > 0);
	return d2;
}

bool Tara(int Z)
{
	for(int i = 0; i < 8; i++)
	{
		for(int j = 0; j < 8; j++)
		{
			if(board[i][j][Z] == 0) return true;
		}
	}
	return false;
}

int automatic()
{
	Clear();
	Place(0, 0, 0);
	bool br = false;
	vector<int> nums = {0,1,2,3,4,5,6,7};
	vector<int> zCoords;
	
	for(int i = 0; i < 4; i++) 
	{
		int r = rand()%nums.size();
		zCoords.push_back(nums.at(r)); nums.erase(nums.begin() + r);
	}
	for(int z : zCoords)
	{
		for(auto k : randomVec())
		{
			if(k == 0)if(Place(0, 0, z)) {z = 0; break;}
			if(k == 1)if(Place(0, 7, z)) {z = 0; break;}
			if(k == 2)if(Place(7, 7, z)) {z = 0; break;}
			if(k == 3)if(Place(7, 0, z)) {z = 0; break;}
		}
	}
	for(int z = 0; z < 8; z++)
	{
		br = false;
		for(int i = 0; i < 8; i++)
		{
			for(auto k : randomVec())
			{
				if(k == 0)if(Place(0,i,z)) {z = 0;break; br = true;}
				if(k == 1)if(Place(i,0,z)) {z = 0;break; br = true;}
				if(k == 2)if(Place(7,i,z)) {z = 0;break; br = true;}
				if(k == 3)if(Place(i,7,z)) {z = 0;break; br = true;}
			}
			if(br)break;
		}
	}
	vector<int> coords_x, coords_y;
	vector<int> avalibleZ = {0,1,2,3,4,5,6,7};
	int randomZ = 0;
	do
	{
		randomZ = 0;
		coords_x.clear();
		coords_y.clear();
		int index_ = rand()%avalibleZ.size();
		randomZ = avalibleZ.at(index_);
		avalibleZ.erase(avalibleZ.begin() + index_);
		
		for(int i = 0; i < 8; i++)
		{
			for(int j = 0; j < 8; j++)
			{
				if(board[randomZ][j][i] == 0)
				{
					coords_x.push_back(i);
					coords_y.push_back(j);
				}
			}
		}
		
		int first_size = coords_x.size();
		for(int k = 0; k < first_size; k++)
		{
			int index = rand()%coords_x.size();
			int gX = coords_x.at(index);
			int gY = coords_y.at(index);
			Place(gX, gY, randomZ);
			coords_x.erase(coords_x.begin() + index);
			coords_y.erase(coords_y.begin() + index);
		}
	}while(!avalibleZ.empty());
	
	return moves;
}

int main()
{
	srand(time(NULL));
	Clear();
	string command;
	while(true)
	{
		ClearScreen();
		print();
		getline(cin, command);
	    string word;
	    vector<string> words;
	    istringstream iss(command);
	    while(iss >> word) words.push_back(word);
		words.push_back(word);
	    for(int i = 0; i < words[0].length(); i++) words[0][i] = tolower(words[0][i]);
	    words.pop_back();
	    if(words[0] == "vezir" && words.size() == 4)
	    {
	    	int X = words[1][0] - 'a', Y = words[2][0] - '0', Z = stoi(words[3]);
	    	Place(X, Y-1, Z);
		}
		else if(words[0] == "clear") Clear();
		else if(words[0] == "auto")
		{
			int maxK = 1;
			if(words.size() > 1) maxK = atoi(words[1].c_str());
			
			int max = 0;
			for(int k = 0; k < maxK; k++)
			{
				int moves = automatic();
				if(moves > max) max = moves;
				if(words.size() > 2 && words[2] == "write")write_to_file(toString(max));
			}
			cout << "Maksimum: " << max << endl << endl;
			system("pause");
		}
		else if(words[0] == "help")
		{
			cout << "Vezir Command: Vezir x(a-h) y(1-8) z(0-7)" << endl << "clear Command: clears the entire board" << endl << endl;
			system("pause");
		}
	}
	return 0;
}

CevapAlıntı
Topic Tags
sonsuz
(@sonsuz)
Admin
Katılım : 4 sene önce
Gönderiler: 42
07/12/2019 11:07 am  

Yunus Emre Aydın çözümü yukarıdaki koddadır.

auto 1000 

yazarsanız programı 1000 çözümden en yüksek değerli olanları seçer.


CevapAlıntı

Cevap yaz


 
Preview 0 Revisions Saved
Share: