Блог пользователя kavishrox

Автор kavishrox, 11 лет назад, По-английски

I made the following code for the problem http://www.spoj.com/problems/HIKE/ but its showing Wrong Answer and I am not able to find the case for which its going wrong . It would be very helpful if someone gives the case for my solution goes wrong.

#include<iostream>
#include<cstdio>
#include<vector>
#include<utility>
#include<string>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<queue>

using namespace std;

vector<vector<char> > v;
int dp[51][51][51];
queue<int> q;
int n;
void bfs(int a,int b,int c){
	a--,b--,c--;
	q.push(a);
	q.push(b);
	q.push(c);
	dp[a][b][c]=0;
	//cout<<dp[a][b][c]<<endl;
	while(!(q.empty())){
		int x=q.front();
		q.pop();
		int y=q.front();
		q.pop();
		int z=q.front();
		q.pop();
		for(int i=0;i<n;i++){
			if(v[x][i]==v[y][z] and dp[i][y][z]>dp[x][y][z]+1){
				q.push(i);
				q.push(y);
				q.push(z);
				dp[i][y][z]=dp[x][y][z]+1;
			}
			else if(v[y][i]==v[x][z] and dp[x][i][z]>dp[x][y][z]+1){
				q.push(x);
				q.push(i);
				q.push(z);
				dp[x][i][z]=dp[x][y][z]+1;
			}
			else if(v[z][i]==v[x][y] and dp[x][y][i]>dp[x][y][z]+1){
				q.push(x);
				q.push(y);
				q.push(i);
				dp[x][y][i]=dp[x][y][z]+1;
			}
		}
	}
}
int main(){
	
	while(cin>>n and n){
		v.resize(n);
		for(int i=0;i<n;i++){
			v[i].resize(n);
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				for(int k=0;k<n;k++){
					dp[i][j][k]=INT_MAX-10;
				}
			}
		}
		int a,b,c;
		cin>>a>>b>>c;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){	
				cin>>v[i][j];
			}
		}
		/*for(int i=0;i<n;i++){
			cout<<endl;
			for(int j=0;j<n;j++){
				cout<<v[i][j]<<" ";
			}
		}*/
		bfs(a,b,c);
		int sol=INT_MAX-10;
		for(int i=0;i<n;i++){
			//cout<<dp[i][i][i]<<endl;
			if(dp[i][i][i] < sol)
				sol=dp[i][i][i];
		}
		if(sol==INT_MAX-10)
			cout<<"impossible"<<endl;
		else
			cout<<sol<<endl;
	}
	return 0;
}
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
11 лет назад, # |
  Проголосовать: нравится -19 Проголосовать: не нравится

wtf? while(cin>>n and n)

while (cin >> n) {
   if (!n) break;
}
  • »
    »
    11 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I don't think there's any problem with cin>>n and n but yes I will try to incorporate what you said in my answers.

    • »
      »
      »
      11 лет назад, # ^ |
      Rev. 3   Проголосовать: нравится -19 Проголосовать: не нравится

      you are wrong.
      1. what is "and" ?
      2. operator >> in std::basic_istream<_Elem,_Traits> returns std::basic_istream<_Elem,_Traits>& and std::basic_istream<_Elem,_Traits> has operator void*
      what is voidPtrVar and intVar?
      voidPtrVar && (void*)intVar ?
      voidPtrVar & (void*)intVar ?
      (int)voidPtrVar && intVar ?
      (int)voidPtrVar & intVar ?

      • »
        »
        »
        »
        11 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        and is equivalent to && in GNU C++ as said below by bayleef and as far as I know about the correctness of cin>>n thing, an if statement needs either a boolean, an integer, or a pointer as the quantity to be tested. The result of std::cin >> x is a reference to an istream, which is none of the above. However, the class istream does have that operator void* conversion operator which can be used to transform the istream reference to a pointer. It is this conversion operator that the language uses for the if test. Since failure to read marks the stream as invalid, the if test will fail if the read didn't work.

  • »
    »
    11 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    And what's the problem with "and"? In GNU C++ it's equivalent of &&.