How to swap elements in 2d array c++ - c++

I'm trying to make a 15 puzzle game by swapping an element with the position of a blank element. I've made this code, but it only swaps when the tile is to the right or below for some reason.
void moveTile(int gameBoard[][SIZE], int nextMove, int &blanki, int &blankj)
{
int temp=nextMove;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(gameBoard[i][j]==nextMove)
{
gameBoard[i][j]=gameBoard[blanki][blankj];
gameBoard[blanki][blankj]=temp;
blanki=i;
blankj=j;
}
}
}
}

Walk thru it in a debugger. You'll see that after a swap is made, you keep going and searching. In the case where the tile is above or left, you'll swap a second time.
You need to stop searching once you've made a swap.

I have updated it to include a stop, now the code does not iterate.
void moveTile(int gameBoard[][SIZE], int nextMove, int &blanki, int &blankj)
{ bool stop=false;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
while(!stop)
{if(gameBoard[i][j]==nextMove)
{
int temp=gameBoard[i][j];
gameBoard[i][j]=gameBoard[blanki][blankj];
gameBoard[blanki][blankj]=temp;
blanki=i;
blankj=j;
stop=true;
}
}
}
}
}

Related

How to solve 8-puzzle problem using Breadth-First search in C++

I want to build a c++ program that would solve 8-puzzle problem using BFS.
I want to show every generated state.
But the problem is, I don't know how to generate state.
I just want some clean function which will efficiently generate states and there will be a Explored array which will assure that there is no redundant state.
I've explored GitHub but there is too much complex solutions
I've written the following code till now
#include<iostream>
#include<conio.h>
using namespace std;
class puzzle{
private:
int initial[3][3],goal[3][3] = {{1,2,3},{4,5,6},{7,8,0}};
int queue[1000];
string data;
public:
void genratePuzzle();
void showState();
bool check_goal(int initial);
};
void puzzle::genratePuzzle(){
cout<<"\n***Create initial state 0-8***\n";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<"Insert at ["<<i<<"]["<<j<<"] : ";
cin>>initial[i][j];
}
}
}
void puzzle::showState(){
cout<<"\n***State***\n";
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
cout<<initial[i][j]<<" ";
}
cout<<endl;
}
}
bool puzzle::check_goal(int initial){
bool check = true;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(initial[i][j] != goal[i][j]){
check = false;
}
}
}
return check;
}
int main(){
puzzle p1;
p1.genratePuzzle();
p1.showState();
getch();
}
Goal state
1 2 3
4 5 6
7 8 0
Put your state into
struct state {
int data[3][3];
bool operator < (const state & other) {
for (int y=0; y<3; ++y) {
for (int x=0; x<3; ++x) {
if (data[y][x] < other.data[y][x]) {
return true;
}
if (data[y][x] > other.data[y][x]) {
return false;
}
}
}
return false; // all were equal
}
}
Now you can use values of type state as keys in a std::map e.g. make a std::map<state, bool> explored if you want. It behaves like an array indexed by states, so:
state a;
// do something to the state...
// and you can do this
explored[a] = true;
How do you generate new states? You start with an existing state and try all valid moves on it. Repeat until done.

I'am trying to implement BFS and count all the visited node through the edges. But my code giving me 0 count except the 1st one

In this code I just want to visit the node and count the edges. For the 1st time it seems good but when I pass new nodes and edges it is giving 0 count. I find out that it's condition is not true for the next nodes and edges. It's my first implementation of BFS.
#include<bits/stdc++.h>
using namespace std;
vector<int>v[1000];
int level[1000];
bool vis[1000];
void bfs(int s,int E)
{
int count=0;
queue<int>q;
q.push(s);
level[s]=0;
vis[s]=true;
while(!q.empty())
{
int p=q.front();
q.pop();
for(int i=0;i<v[p].size();i++)
{
if(vis[v[p][i]] == false)
{
level[v[p][i]] = level[p]+1;
q.push(v[p][i]);
vis[v[p][i]] = true;
count++;
}
}
}
cout<<count<<endl;
}
int main()
{
int N,E,x,y,size;
while(scanf("%d %d",&N,&E)==2)
{
for(int i=1;i<=E;i++)
{
scanf("%d %d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
int s=0;
bfs(s);
}
return 0;
}
You are not resetting whatever variables you have used like your adjacency list v,level and vis.
You have to reset them to some default value before working on a different graph, as values of previous graphs are unwanted.
You can simply run a loop, before each input:
for(int i=0;i<N;i++)
{
v[i].clear();
vis[i]=0;
level[i]=-1;
}

Manipulating data in struct format ends up weird

So I am working on a maze generator program as a dfs practice.
struct Point{
Point *left,*right,*up,*down;
int x,y;
bool visited;
Point(int n,int m)
:x(n),y(m),visited(false),left(NULL),right(NULL),up(NULL),down(NULL)
{}
};
and I have used a for loop to initialize a vector of vector of Point,
each with a unique address and the visited value assigned as false.
vector<vector<Point*> > board;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
Point *temppt=new Point(j,i);
tempv.push_back(temppt);
}
board.push_back(tempv);
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(i!=0)board[i][j]->up=board[i-1][j];
if(i!=row-1)board[i][j]->down=board[i+1][j];
if(j!=0)board[i][j]->left=board[i][j-1];
if(j!=col-1)board[i][j]->right=board[i][j+1];
}
}
However, something weird happens when I manipulate them during a dfs search...
whenever I execute this segment of code
board[now->y][now->x]->visited=true;
each of the first value of the vector of board (board[x][0]) are also changed to true, as a checked it with this for loop.
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<board[i][j]->visited;
}
cout<<endl;
}
What should I do to manipulate the points one of one instead of column by column
The full dfs function:
void dfs(Point* now,Point* prev,int cnt){
int dir,back;
if(now->visited!=true){
cnt++;
if(prev!=NULL){
if(prev->up==now){
hwall[now->x][now->y]=false;
}
else if(prev->down==now){
hwall[prev->x][prev->y]=false;
}
else if(prev->right==now){
vwall[prev->y][prev->x]=false;
}
else vwall[now->y][now->x]=false;
}
board[now->y][now->x]->visited=true;
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<board[i][j]->visited;
}
cout<<endl;
}
if(cnt<row*col){
back=rand()%10;
vector<Point*> temp;
if(back<5){
A:;
if(now->up!=NULL)temp.push_back(now->up);
if(now->right!=NULL)temp.push_back(now->right);
if(now->down!=NULL)temp.push_back(now->down);
if(now->left!=NULL)temp.push_back(now->left);
dfs(temp[rand()%temp.size()],now,cnt);
}
else{
if(now->up!=NULL&&now->up->visited!=true)temp.push_back(now->up);
if(now->right!=NULL&&now->right->visited!=true)temp.push_back(now->right);
if(now->down!=NULL&&now->down->visited!=true)temp.push_back(now->down);
if(now->left!=NULL&&now->left->visited!=true)temp.push_back(now->left);
if(temp.size()==0)goto A;
dfs(temp[rand()%temp.size()],now,cnt);
}
}
}
The full constructor:
Maze(int n,int m):row(n),col(m){
int i,j;
vector<Point*> tempv;
for(i=0;i<row;i++)vwall.push_back(vector<bool>(col-1,true));
for(i=0;i<col;i++)hwall.push_back(vector<bool>(row-1,true));
for(i=0;i<row;i++){
for(j=0;j<col;j++){
Point *temppt=new Point(j,i);
tempv.push_back(temppt);
}
board.push_back(tempv);
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(i!=0)board[i][j]->up=board[i-1][j];
if(i!=row-1)board[i][j]->down=board[i+1][j];
if(j!=0)board[i][j]->left=board[i][j-1];
if(j!=col-1)board[i][j]->right=board[i][j+1];
}
}
dfs(board[0][0],NULL,0);
}
You aren't clearing tempv after you push it back into board. So each entry of board is receiving an extended copy of tempv. (Specifically, the first col entries will be identical in each row.)
Just call tempv.clear() right after board.push_back(tempv), so that each row starts with an empty vector.

Different output when set different breakpoints

I just wrote a code to build a Huffman Tree using MinHeap. When testing I want to output its traversal result.
The algorithm is simple, but my code can't get the right answer. It's strange that the output was different when I set different breakpoints. For instance, it depends on if I set a break point in the loop, such as line 165 input_list.insert(*parent);.
The test input was
4 //number of nodes.
1 1 3 5 //weight of each node.
and the output when debugging it with a breakpoint in the loop is
5
10
1
2
1
5
3
that is correct. But when I just run it without debug, it even didn't have any output. Does anyone know how to explain it?
#include <iostream>
#include <vector>
using namespace std;
#define max_size 100
int sum=0;
class huffman_node
{
public:
int weight;
huffman_node* left_child;
huffman_node* right_child;
huffman_node(){}
huffman_node(int w, huffman_node* l, huffman_node* r):
weight(w),left_child(l),right_child(r) {}
};
vector <huffman_node> node_list;
class minheap
{
public:
minheap()
{
heap=new huffman_node [max_size];
current_size=0;
}
~minheap()
{
delete []heap;
}
void siftdown(int start, int m)
{
int i=start;
int j=2*i+1;
huffman_node temp=heap[i];
while(j<=m)
{
if(j<m && heap[j+1].weight<heap[j].weight)
{
++j;
}
if(temp.weight<=heap[j].weight)
{
break;
}
else
{
heap[i]=heap[j];
i=j;
j=2*i+1;
}
}
heap[i]=temp;
}
void siftup(int start)
{
int j=start;
int i=(j-1)/2;
huffman_node temp=heap[j];
while(j>0)
{
if(heap[i].weight<=temp.weight)
{
break;
}
else
{
heap[j]=heap[i];
j=i;
i=(j-1)/2;
}
heap[j]=temp;
}
}
bool insert(const huffman_node& input)
{
if(current_size==max_size)
{
cout<<"minheap full"<<endl;
return false;
}
heap[current_size]=input;
siftup(current_size);
++current_size;
return true;
}
bool remove_min(huffman_node& output)
{
if(!current_size)
{
cout<<"minheap empty"<<endl;
return false;
}
output=heap[0];
heap[0]=heap[current_size-1];
--current_size;
siftdown(0,current_size-1);
return true;
}
private:
huffman_node* heap;
int current_size;
};
void route_length(huffman_node* &root,int depth)
{
if(root!=NULL)
{
// if(root->left_child==NULL&&root->right_child==NULL)
// {
// sum+=depth*root->weight;
// }
route_length(root->left_child,depth+1);
cout<<root->weight<<endl;
route_length(root->right_child,depth+1);
}
else
{
return;
}
}
int main()
{
minheap input_list;
int n;
cin>>n;
for(int i=0;i<n;++i)
{
int key;
cin>>key;
huffman_node input(key,NULL,NULL);
input_list.insert(input);
cin.get();
}
huffman_node* root;
for(int i=0;i<n-1;++i)
{
huffman_node* parent;
huffman_node out1;
huffman_node out2;
input_list.remove_min(out1);
input_list.remove_min(out2);
node_list.push_back(out1);
node_list.push_back(out2);
parent=new huffman_node(out1.weight+out2.weight,&node_list[node_list.size()-2],&node_list[node_list.size()-1]);
input_list.insert(*parent);
root=parent;
}
route_length(root,0);
// cout<<sum<<endl;
return 0;
}
The problem is that you are using pointers to elements of a vector<huffman_node> and storing these in your data structure (i.e. left and right members of the huffman_node object).
The thing that is randomly killing your program is that std::vector moves values around in memory when you append to it. The contents of the elements of the vectors are preserved, but the location is not. Once it moves the elements, the memory where the vector used to be can be overwritten by whatever (i.e. gdb needs heap memory too) and now the pointers are pointing to garbage.
As a quick sanity check, you can make your code not crash by reserving space in your node_list by calling
node_list.reserve(max_size*2);
in the beginning of main. This is not the right way of developing this piece of code further, but should illustrate the problem.
It would be better if your node_list was a vector<huffman_node*> instead. Or if you changed the left/right members to be vector indices instead of pointers.

My knight's tour algorithm possibly is running on an infinite loop

Here's the code i wrote.
#include "genlib.h"
#include <iostream>
#include <math.h>
#include "vector.h"
struct square
{
int x;
int y;
};
bool knighttour(square start,int &counter,int cb[][8]);
Vector <square> generatemoves (square start);
void Marksquare(int &cb,int ctr);
void Unmarksquare(int &cb);
bool IsLegal(square a,int cb[][8]);
int main()
{
int chessboard[8][8];
for (int i=0;i<8;i++)
for (int j=0;j<8;j++)
chessboard[i][j]=-1;
int counter=1;
for (int i=0;i<8;i++){
for (int j=0;j<8;j++){
square temp;
temp.x=i;
temp.y=j;
if (knighttour(temp,counter,chessboard))
{
for (int k=0;k<8;k++){
cout<<chessboard[k][0]<<chessboard[k][1]<<chessboard[k][2]<<chessboard[k][3]<<chessboard[k][4]<<chessboard[k][5];
cout<<chessboard[k][6]<<chessboard[k][7]<<endl;}
}
}
}
return 0;
}
bool knighttour(square pt,int &counter,int cb[][8])
{
Marksquare(cb[pt.x][pt.y],counter);
if (counter==64)
return true;
counter++;
Vector <square> temp = generatemoves(pt);
for (int i=0;i<temp.size();i++)
{
if (IsLegal(temp[i],cb))
knighttour(temp[i],counter,cb);
}
Unmarksquare(cb[pt.x][pt.y]);
counter--;
return false;
}
Vector <square> generatemoves (square start)
{
Vector <square> temp;
Vector <square> temp2;
square mv1;
mv1.x=start.x+2;
mv1.y=start.y+1;
temp.add(mv1);
square mv2;
mv2.x=mv1.x;
mv2.y=start.y-1;
temp.add(mv2);
square mv3;
mv3.y=start.y+2;
mv3.x=start.x+1;
temp.add(mv3);
square mv4;
mv4.y=start.y+2;
mv4.x=start.x-1;
temp.add(mv4);
square mv5;
mv5.x=start.x-2;
mv5.y=start.y+1;
temp.add(mv5);
square mv6;
mv6.x=start.x-2;
mv6.y=start.y-1;
temp.add(mv6);
square mv7;
mv7.y=start.y-2;
mv7.x=start.x-1;
temp.add(mv7);
square mv8;
mv8.y=start.y-2;
mv8.x=start.x+1;
temp.add(mv8);
for (int i=0;i<temp.size();i++)
if (temp[i].x>=0 && temp[i].x<=7 && temp[i].y>=0 && temp[i].y<=7)
temp2.add(temp[i]);
return temp2;
}
void Marksquare(int &a,int ctr)
{
a=ctr;
}
void Unmarksquare(int &a)
{
a=-1;
}
bool IsLegal(square a,int cb[][8])
{
if (cb[a.x][a.y]==-1)
return true;
else
return false;
}
A little explanation. I am using an int [8][8] to represent the board of chess and initially i put in every square of the board the number -1.
As the Knight moves it marks the square that he visits with the counter (int counter) and from there (and for all the legal moves the knight can take) makes recursive calls to find a path (the goal is to visit each square exactly once).
Once the counter hits 64 the function bool knighttour(square start,int &counter,int cb[][8])
must return true and the main program then should display "the knight's tour" as it is marked on the [8][8] chessboard.
I believe that the above code i provided runs on an infinite loop. I let it run for 3 minutes.
Theory says:
...It is important to note that an exhaustive brute force approach (one which iterates through all possible move sequences) can never be applied to the Knight's Tour problem (except for very small board sizes). For a regular 8x8 chess board, there are approximately 4×1051 possible move sequences,[9] and it would take an unfathomable amount of time to iterate through such a large number of moves.
So to ensure that your program works, try with smaller board size (say 4x4).
To ensure your program works for 8x8 in reasonable time you'll have to change the algorithm. There are many in addition to those listed here.
--edit--
also to ensure that your program is doing something, it's always a good idea to add some traces while you're still developing it.
E.g.
bool knighttour(square pt,int &counter,int cb[][8]) {
printf("\r%d ", counter); // <<<---
Marksquare(cb[pt.x][pt.y],counter);
if (counter==64)
return true;
counter++;
Vector <square> temp = generatemoves(pt);
for (int i=0;i<temp.size();i++)
{
if (IsLegal(temp[i],cb))
knighttour(temp[i],counter,cb);
}
Unmarksquare(cb[pt.x][pt.y]);
counter--;
return false;
}
This code probably tries to find all possible routes in knights tour and will return last found route.
Instead of
for (int i=0;i<temp.size();i++)
{
if (IsLegal(temp[i],cb))
knighttour(temp[i],counter,cb);
}
Try
for (int i=0;i<temp.size();i++)
{
if (IsLegal(temp[i],cb))
{
if(knighttour(temp[i],counter,cb))
{
return true;
}
}
}
One thing I see is that although you return true when counter==64 in knighttour, that doesn't get propagated, the function calling it will return false.. so you'll never notice it in main().
That said, even if you fix your algorithm it might not finish in your lifetime.