Language: C++


#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
const int N = 8;
const int INF = INT_MAX;

bool hantei(int * box){
	for(int i = 0; i < N; i++){
		if( box[i] != i) return false;
	}
	return true;
}
int zeroIndex(int * box){
	for(int i = 0; i < N; i++){
		if( box[i] == 0) return i;
	}
}

bool canMoveLeft(int * box){
	int index = zeroIndex(box);
	return (index != 0 && index != 4);
}

bool canMoveRight(int * box){
	int index = zeroIndex(box);
	return (index != 3 && index != 7);
}

bool canMoveUp(int *box){
	int index = zeroIndex(box);
	return (index >= 4);
}

bool canMoveDown(int *box){
	int index = zeroIndex(box);
	return (index <= 3);
}
int *copyBox(int *box){
	int *newBox = new int[N];
	for(int i = 0; i < N; i++){
		newBox[i] = box[i];
	}
	return newBox;
}
int *swap(int *box,int x,int y){
	int temp;
	int *newBox = copyBox(box);
	temp = box[x];
	newBox[x] = newBox[y];
	newBox[y] = temp;
	return newBox;
}
int *moveLeft(int *box){
	int zeroInd = zeroIndex(box);
	return swap(box,zeroInd,zeroInd - 1);
}
int *moveRight(int *box){
	int zeroInd = zeroIndex(box);
	return swap(box,zeroInd,zeroInd + 1);
}
int *moveUp(int *box){
	int zeroInd = zeroIndex(box);
	return swap(box,zeroInd,zeroInd - 4);
}

int *moveDown(int *box){
	int zeroInd = zeroIndex(box);
	return swap(box,zeroInd,zeroInd + 4);
}

void printBox(int *box){
	for(int i = 0; i < N; i++){
		cout << box[i] <<" ";
	}
	cout << endl;
}
bool isEqualBox(int *box1, int *box2){
	for(int i = 0; i < N; i++){
		if(box1[i] != box2[i]){
			return false;
		}
	}
	return true;
}

bool didPass(vector<int *> oldBoxList,int *box){
	auto itr = oldBoxList.begin();
	while( itr != oldBoxList.end()){
		if( isEqualBox( *itr,box) == true){
			return true;	
		}
		itr++;
	}
	return false;
}

int wfs(int *box){
	if(hantei(box) == true){
		return 0;
	}
	int count = 0;
	int *currentBox;
	queue<pair<int*,int>> nextBoxList;
	vector<int *> oldBoxList;
	pair<int*,int> p(box,count);
	nextBoxList.push(p);
	oldBoxList.push_back(box);
	while( nextBoxList.empty() != true){
		pair<int*,int> currentPair;
		currentPair = nextBoxList.front();
		currentBox = currentPair.first;
		int currentNum = currentPair.second;
		nextBoxList.pop();
		//cout<< currentNum << " : ";
		//printBox(currentBox);
		//can move left ?
		if( canMoveLeft(currentBox)){
			int *leftBox = moveLeft(currentBox);
			//if the leftBox is goal state, return count value
			if(hantei( leftBox) == true){
				return currentNum + 1;
			}else if(didPass(oldBoxList,leftBox) != true){
			//if leftBox have not appeared , add to the nexBoxList and oldBoxList
				pair<int *,int> leftPair(leftBox,currentNum + 1);
				nextBoxList.push(leftPair);
				oldBoxList.push_back(leftBox);
			}
		}
		//can move right?
		if( canMoveRight(currentBox)){
			int *rightBox = moveRight(currentBox);
			//if the leftBox is goal state, return count value
			if(hantei( rightBox) == true){
				return currentNum + 1;
			}else if(didPass(oldBoxList,rightBox) != true){
			//if leftBox have not appeared, add to the nexBoxList and oldBoxList
				pair<int*,int> rightPair(rightBox,currentNum + 1);
				nextBoxList.push( rightPair);
				oldBoxList.push_back(rightBox);
			}
		}
		//can move up?
		if( canMoveUp(currentBox)){
			int *upperBox = moveUp(currentBox);
			//if the uppertBox is goal state, return count value
			if(hantei( upperBox) == true){
				return currentNum + 1;
			}else if(didPass(oldBoxList,upperBox) != true){
			//if upperBox have not appeared, add to the nexBoxList and oldBoxList
				pair<int*,int> upperPair(upperBox,currentNum + 1);
				nextBoxList.push( upperPair);
				oldBoxList.push_back(upperBox);
			}
		}
		//can move down?
		if( canMoveDown(currentBox)){
			int *lowerBox = moveDown(currentBox);
			//if the leftBox is goal state, return count value
			if(hantei( lowerBox) == true){
				return currentNum + 1;
			}else if(didPass(oldBoxList,lowerBox) != true){
			//if leftBox have not appeared, add to the nexBoxList and oldBoxList
				pair<int *,int> lowerPair(lowerBox,currentNum + 1);
				nextBoxList.push( lowerPair);
				oldBoxList.push_back(lowerBox);
			}
		}

	}
	return -1;
}
int main()
{
	int a[8];
	int input;
	int index = 0;
	while(cin >> input){
		a[index] = input;
		index++;
		if(index == 8){
			cout << wfs(a) << endl;
			index = 0;
		}
	}
	return 0;
}

Compiler output:
a.cc:10:17: error: 'INT_MAX' was not declared in this scope
   10 | const int INF = INT_MAX;
      |                 ^~~~~~~
a.cc:7:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    6 | #include <queue>
  +++ |+#include <climits>
    7 | #include <vector>
a.cc: In function 'int zeroIndex(int*)':
a.cc:22:1: warning: control reaches end of non-void function [-Wreturn-type]
   22 | }
      | ^