ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1603. Erudite

WA3 here is my Code Help ME
Posted by fuch_prog_er 2 Mar 2009 00:47
#include <string>
#include <iostream>
#include <stack>
using namespace std;
char a[6][6];
int b[5][5];
int i,j,n,o,q,p,r,m;
stack <int> si,sj;
bool t=false;
string s;
//******************************************
void inp(){
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
{
cin>>a[i][j];
}
for(m=0;m<5;m++)
{a[m][0]=a[0][m]='-1';
a[m][5]=a[5][m]='-1';
}
}
//******************************************
void copy(){
for(o=1;o<=4;o++)
for(q=1;q<=4;q++)
b[o][q]=1;
}
//******************************************
bool check(string buf){
copy();
for(i=1;i<=4;i++){
for(j=1;j<=4;j++){
if(a[i][j]==buf[p]&&b[i][j]==1)
{p=0;
t=true;
si.push(i);
sj.push(j);
p++;
if(p>=buf.length()) return true;
b[i][j]=0;
}
while(!si.empty()){
/*1*/ if(a[si.top()][sj.top()+1]==buf[p]&&b[si.top()][sj.top()+1]==1){
si.push(si.top());
sj.push(sj.top()+1);
t=true;
p++;
if(p>=buf.length()) return true;
b[si.top()][sj.top()]=0;
}
/*2*/ else if(a[si.top()][sj.top()-1]==buf[p]&&b[si.top()][sj.top()-1]==1){
si.push(si.top());
sj.push(sj.top()-1);
t=true;
p++;
if(p>=buf.length()) return true;
b[si.top()][sj.top()]=0;
}
/*3*/ else if(a[si.top()+1][sj.top()]==buf[p]&&b[si.top()+1][sj.top()]==1){
si.push(si.top()+1);
sj.push(sj.top());
t=true;
p++;
if(p>=buf.length()) return true;
b[si.top()][sj.top()]=0;
}
/*4*/ else
if(a[si.top()-1][sj.top()]==buf[p]&&b[si.top()-1][sj.top()]==1){
si.push(si.top()-1);
sj.push(sj.top());
t=true;
p++;
if(p>=buf.length()) return true;
b[si.top()][sj.top()]=0;
}
if(!t){
si.pop();
sj.pop();
if(p!=0)
p--;
}
t=false;
}//<while();
if(si.empty())
copy();
}
}
return false;
}
//******************************************
int main()
{
inp();
cin>>n;
cin.ignore();
for(r=1;r<=n;r++)
{
cin>>s;
if(check(s)) cout<<s<<": YES"<<endl;
else cout<<s<<": NO"<<endl;
}
system("pause");
return 0;
}

Edited by author 02.03.2009 00:54