# Stuck in Volatile Roy problem. Needed help

Problem link : Contest Page | CodeChef

Showing TLE for my code :
“”"
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl “\n”
#define mod 1000000007
#define MAXN 100005

void swap(int& x, int& y)
{int temp = x; x = y; y = temp;}

bool chk(int x)
{
for(int i = 2; i<x; i++)
{
if(x%i==0)
return false;
}
return true;
}

int solve()
{
string s;
int x, y;
for(int i = 0; i<9; i++)
{
cin>>x;
s += ‘0’ + x;
}

string temp = “123456789”;

queue q;
q.push(s);
int ans = 0;
int size;
string t;
set st;
while(!q.empty())
{
ans++;
size = q.size();
for(int j = 0; j<size; j++)
{
auto p = q.front();
q.pop();

``````     if(p==temp)
return ans-1;

for(int i = 0; i<9; i++)
{
t = p;
x = t[i] - '0';
{
y = t[it] - '0';
if(chk(x+y))
{
swap(t[i], t[it]);
if(st.find(t)==st.end())
q.push(t);
}
}
}
}
``````

}
return -1;
}

signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int t = 1;
cin>>t;
while(t–)
{
int x = solve();
cout<<x<<endl;
}
return 0;
}
“”"