A puzzle game from codechef [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Could somebody explain the solution to this problem in cpp and how it works
Can this be made more efficient using STL?
http://www.codechef.com/problems/H1
It would very helpful if you could explain this particular logic along with its time complexity.
#include <cstdio>
using namespace std;
int board[9],q[178000]={123456789},powers[9]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
int posn,fromposn,front=-1,back=1;
char found[98765435]={0};
bool prime[18]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1};
void moveit(int p1,int p2){
int diff=board[p2]-board[p1];
posn=fromposn+diff*powers[p1]-diff*powers[p2];
int posndiv10=posn/10;
if(!found[posndiv10]){
found[posndiv10]=found[fromposn/10]+1;
q[back++]=posn;
}
}
int main(){
int t,i,d;
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
found[12345678]=1;
while(++front<back){
for(posn=fromposn=q[front],i=8;i>=0;i--){
board[i]=posn%10;
posn/=10;
}
if(prime[board[0]+board[1]]) moveit(0,1);
if(prime[board[0]+board[3]]) moveit(0,3);
if(prime[board[1]+board[2]]) moveit(1,2);
if(prime[board[1]+board[4]]) moveit(1,4);
if(prime[board[2]+board[5]]) moveit(2,5);
if(prime[board[3]+board[4]]) moveit(3,4);
if(prime[board[3]+board[6]]) moveit(3,6);
if(prime[board[4]+board[5]]) moveit(4,5);
if(prime[board[4]+board[7]]) moveit(4,7);
if(prime[board[5]+board[8]]) moveit(5,8);
if(prime[board[6]+board[7]]) moveit(6,7);
if(prime[board[7]+board[8]]) moveit(7,8);
}
scanf("%d",&t);
while(t--){
for(posn=i=0;i<8;i++){
scanf("%d",&d);
posn=posn*10+d;
}
scanf("%d",&d);
if(found[posn]) printf("%d\n",found[posn]-1);
else puts("-1");
}
return 0;
}

It first builds a table of all the solvable boards, represented as numbers, that says how many steps from the solution that board is.
Then it solves each test case by looking it up in that table.
The time complexity per test case is constant.
I doubt that there is anything in the standard library that could make it more efficient.

Related

c++:how do i add user input in while loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
how do i add a user input to my code? I want to create a simple while loop program that will increment the value input by the user by 2.
#include <iostream>
using namespace std;
int main()
{
int a=2;
while(a<=100)
{
cout<< "Value of a: "<<a<<endl;
a=a+2;
}
return 0;
}
Not sure I completely understand your question, but the syntax for user input would be cin >> variable here;

What did I do wrong? C++ Newbie here [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I thought the output would be 70? (20+20+20+10=70) Why is it so large?
#include <iostream>
using namespace std;
int main()
{
int a,b,c=20;
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
The issue is that you are not initializing the variables a and b. That means when you attempt to run your program, the computer is looking in memory for a value to use for each, and that number could be very big or very small. Try this:
#include <iostream>
using namespace std;
int main()
{
int a = 20,b = 20,c=20; //here, a and b are defined
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
C is the only one variable that you initialize to 20, the other 2 variables
(a and b) are holding garbage..
so your math calculation is undefined behaviour.

Wrong answer in UVa online judge [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was trying to solve a really simple problem on UVa online judge. The problem code is: 10071. You can find the problem here: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94
My code looks like this:
#include<iostream>
using namespace std;
int main(){
int v,t,a,d;
cin >> v >> t;
t = t*2;
d = (v)*t;
cout << d << endl;
}
But it says wrong answer. What went wrong and how to solve it?
You have not read the complete question.
Correct solution is as follows:
#include <stdio.h>
int main()
{
int a,b,c;
while(scanf("%d%d",&a,&b)==2)
{
printf("%d\n",(a*b)*2);
}
return 0;
}
As you may notice above, there can be multiple test cases. You have to account for it. So I have a while loop for it.

optimizing code performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi i am kind of new in c++. I was trying to optimize my code. My code includes two for loops with a if block inside the second loop. first loop will iterate for 10^14 times and inner for loop will iterate for 10^4 times. My code is as folows
#include<iostream>
using namespace std;
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<stdio.h>
#include<fstream>
using namespace std;
#include<math.h>
#include<thread>
using namespace std;
signed long long run,i,j;
int main()
{
run=0;
for (i=0;i<100000000000000;i++)
{
for (j=0;j<10000;j++)
{
run=run+1;
}
}
cout<<run<<"\n";
}
time it is takling to complete is around 1 day. So I was using thread in my code to make it first. But it is showing to include -std=c++0x. So where to include this?
Is there anyone who would like to help me out?
I'd optimise it as follows, but good compilers might do that anyway:
#include<iostream>
signed long long run,i,j;
int main()
{
i = 100000000000000;
j = 10000;
run = i * j;
std::cout << run << '\n';
}
-std=c++0x is a compiling option, and it tells the compiler you are using C++11 standard.
You might want to take a look at a link like this one http://www.cs.cf.ac.uk/Dave/C/node3.html regarding compiling.

Is there a efficient way to do multiple test cases in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use MS Visual Studio and I am new to C++, so I am just wondering if there is an faster more efficient way to do multiple test cases instead of keep clicking CTRL+F5 and re-opening the console many times.
Like for example if I have this code
#include <iostream>
using namespace std;
void main ()
{
int x;
cout<<"Enter a number"<<endl;
cin>>x;
cout<<x*2<<endl;
}
Is there a way I could try different values of x at once and getting the results together?
Thanks
Simple work around:
while(terminating_condition_is_not_met)
{
execute_what_you_want
}
terminating condition can be EOF or max_no_of_iterations or some_sentinel_value
And for your code, I used -1 as a sentinel.
#include <iostream>
using namespace std;
void main ()
{
int x;
while(1)
{
cout<<"Enter a number"<<endl;
cin>>x;
if(x==-1)
break;
cout<<x*2<<endl;
}
}