Created
February 4, 2020 20:32
-
-
Save tasfik007/9c519120a9e7e4aa3bac7ca67453bdd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define pii pair<int,int> | |
using namespace std; | |
int j1=4,j2=3,d=2; | |
queue<pii> steps; | |
vector<pii> track; | |
map<pii, int> visited; | |
void fill_jars(int cur_j1,int cur_j2) | |
{ | |
steps.push({ cur_j1, j2 }); | |
steps.push({ j1, cur_j2 }); | |
} | |
void empty_jars() | |
{ | |
steps.push({ j1, 0 }); | |
steps.push({ 0, j2 }); | |
} | |
void transfer(int cur_j1,int cur_j2) | |
{ | |
for (int i = 0; i <= max(j1, j2); i++) | |
{ | |
int c = cur_j1 + i; | |
int dd = cur_j2 - i; | |
if (c == j1 || (dd == 0 && dd >= 0)) | |
steps.push({ c, dd }); | |
c = cur_j1 - i; | |
dd = cur_j2 + i; | |
if ((c == 0 && c >= 0) || dd == j2) | |
steps.push({ c, dd }); | |
} | |
} | |
void BFS() | |
{ | |
steps.push({ 0, 0 }); | |
while (!steps.empty()) { | |
pii u = steps.front(); | |
steps.pop(); | |
if (visited[{ u.first, u.second }] == 1) | |
continue; | |
if ((u.first > j1 || u.second > j2 || | |
u.first < 0 || u.second < 0)) | |
continue; | |
track.push_back({ u.first, u.second }); | |
visited[{ u.first, u.second }] = 1; | |
if (u.first == d || u.second == d) { | |
if (u.first == d) { | |
if (u.second != 0) | |
track.push_back({ u.first, 0 }); | |
} | |
else { | |
if (u.first != 0) | |
track.push_back({ 0, u.second }); | |
} | |
int sz = track.size(); | |
for (int i = 0; i < sz; i++) | |
{ | |
cout << "Step "<<i<<"->"<<" Jar-1: " << track[i].first<<" Jar-2: "<< track[i].second << endl<<endl; | |
} | |
} | |
fill_jars(u.first,u.second); | |
transfer(u.first,u.second); | |
empty_jars(); | |
} | |
} | |
int main() | |
{ | |
cout << "Steps to desired output::"<<endl<<endl; | |
BFS(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment