352 - The Seasonal War

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

chetan
New poster
Posts: 43
Joined: Sun Sep 24, 2006 2:39 pm

Post by chetan »

hi all. I am getting RE for this problem
please help me . it works fine on my comp for the sample input.

:cry: :cry: :cry:

Code: Select all


CODE DELETED AFTER AC :D      
    
Last edited by chetan on Sat Jul 14, 2007 6:03 pm, edited 1 time in total.
Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

Remove fflush and use

Code: Select all

scanf("%s",str);
instead of

Code: Select all

gets(str); 
Hope it helps.
Ami ekhono shopno dekhi...
HomePage
chetan
New poster
Posts: 43
Joined: Sun Sep 24, 2006 2:39 pm

Post by chetan »

thanks a lot !!!! :D
SHAHADAT
New poster
Posts: 23
Joined: Thu Jun 22, 2006 8:55 am
Location: sust,bangladesh

Post by SHAHADAT »

BETOVSKY

use your array a[27][27] as char and try to input as

Code: Select all

for(i=1;i<=n;i++)
{ 
	scanf("%s", &a[i][1]); 
} 
	
instead of

Code: Select all

for(i=1;i<=n;i++)
{
     scanf("%d", &linha); 
     for(j=1;j<=n;j++){ 
     a[i][j]=linha%10; 
     linha=linha/10; 
    } 

} 


because int is not sufficient of input as a number of 25 digits.
-----------------------------------------------------------------
LIFE IS COMBINED WITH JOY AND SORROW.IT IS NOT ONLY FOR SITTING IN A ROOM AND TYPING CODE.
quinine
New poster
Posts: 2
Joined: Sat Nov 03, 2007 7:35 pm

Post by quinine »

Shaka_RDR wrote:sorry, my algorithm was wrong... i should've used recursive to solve this problem (but i didn't, cause i want to prove that i can solve this problem using iteration not recursive)...

to any body who still have WA , i suggest you to check with this sample input :

INPUT :

5
00000
01010
01010
01110
00000

OUTPUT :
Image number 1 contains 1 war eagles.
Yes! That was my problem too. I'm curious though, is there any way to make breadth-first scanning (the iteration thing) work for such cases?
vivgrn
New poster
Posts: 6
Joined: Sat Aug 25, 2007 4:48 pm

Re: 352 The Seasonal War

Post by vivgrn »

Can anyone please tell me whats wrong in this code because iam always getting a wrong answer.

Code: Select all

#include<iostream>
#include<vector>
#include<string>

using namespace std;
void func( vector < vector<char>  > &arr,int i,int j,int num);
int main()
{
    int num;
    int xcase=0;
    while(cin>>num)
    {
                   xcase++;
                   const int p=num;
                   vector< vector<char> > arr(p, vector<char> (p));
                   for(int i=0;i<num;i++)
                   {
                           for(int j=0;j<num;j++)
                           {
                                   char ch;
                                   cin>>ch;
                           arr[i][j]=ch;
                           }
                   }
                   int sum=0;
                   for(int i=0;i<num;i++)
                   {
                           for(int j=0;j<num;j++)
                           {
                           if(arr[i][j]=='1')
                           {
                                             sum++;
                                             func(arr,i,j,num);
                           }
                            }
                   }
                   if(sum==1)
                   cout<<"Image number "<<xcase<<" contains "<<sum<<" war eagle."<<endl;
                   else
                   cout<<"Image number "<<xcase<<" contains "<<sum<<" war eagles."<<endl;
                   
    }
return 0;                   
}
    
void func( vector < vector<char>  > &arr,int i,int j,int num)

{
     arr[i][j]='0';
     if(i-1>=0 &&j-1 >=0)
     {
               if(arr[i-1][j-1]=='1')
               func(arr,i-1,j-1,num);
     }
     if(j-1>=0)
     {
               if(arr[i][j-1]=='1')
               func(arr,i,j-1,num);
     }
     if(i+1<num &&j-1>=0)
     {
                if(arr[i+1][j-1]=='1')
                func(arr,i+1,j-1,num);
     }
     if(i-1>0)
     {
        if(arr[i-1][j]=='1')
               func(arr,i-1,j,num);
     }  
     if(i+1<num)
     {
                if(arr[i+1][j]=='1')
                func(arr,i+1,j,num);
     }
     if(j+1<num &&i-1>=0)
     {
              if(arr[i-1][j+1]=='1')
              func(arr,i-1,j+1,num);
     }
     if(j+1<num)
     {
                if(arr[i][j+1]=='1')
                func(arr,i,j+1,num);
     }
     if(j+1<num && i+1 <num)
     {
                if(arr[i+1][j+1]=='1')
                func(arr,i+1,j+1,num);
     }
}
Awaiting your reply

Thanks in advance
golden eye
New poster
Posts: 2
Joined: Sat Jan 03, 2009 9:24 am

Re: 352 The Seasonal War

Post by golden eye »

cut
bm_anas
New poster
Posts: 10
Joined: Fri May 15, 2009 9:13 pm

352 The Seasonal War WA

Post by bm_anas »

hi,could anyone give some input,for that my prog goes wrong.Thnx in advance.

#include<stdio.h>
#include<string.h>

int main()
{
int n,i,j,total,used[30][30],flag,num=1,max,len;
char input[30][30];

while(scanf("%d",&n)==1)
{
for(i=0,max=0;i<n;i++)
{
scanf("%s",input);
len=strlen(input);
if(len>max)
max=len;

}

for(i=0;i<=n;i++)
for(j=0;j<=max;j++)
used[j]=0;

for(i=0;i<n;i++)
{
len=strlen(input);
for(len;len<max;len++)
input[len]='0';
input[len]=0;
}

total=0;

for(i=0;i<n;i++)
{
for(j=0;j<len;j++)
{
if(input[j]=='1')
{
if(i==n-1)//last row
{
if(!used[j])
total++;
if(j!=len-1)
used[j+1]=1;
used[j]=1;
}

else//before last
{
flag=0;
if(!used[i][j])
{
flag=1;


}


if(j==0)//first column
{
if(flag)
total++;
used[i+1][j]=1;
used[i+1][j+1]=1;
}
else if(j==len-1)//last column
{
if(input[i+1][j-1]=='1')
if(used[i+1][j-1])
flag=0;
if(flag)
total++;
used[i+1][j-1]=1;
used[i+1][j]=1;
}
else//middle columns
{
if(input[i+1][j-1]=='1')
if(used[i+1][j-1])
flag=0;
if(flag)
total++;
used[i+1][j]=1;
used[i+1][j-1]=1;
used[i+1][j+1]=1;

}
if(j!=len-1)//not last column.
used[i][j+1]=1;

}


}
}
}
printf("Image number %d contains %d war eagles. \n",num++,total);
}

return 0;
}
saiful_sust
Learning poster
Posts: 97
Joined: Fri Aug 22, 2008 10:18 pm
Location: CSE.SUST.SYLHET

Re: 352 The Seasonal War WA

Post by saiful_sust »

This is a normal floodfill problem...

Try to do this ....

AND After ACC PLZ remove ur code.... :D
mntamim
New poster
Posts: 8
Joined: Wed Nov 06, 2013 12:08 am

352 The Seasonal War wrong answer ??

Post by mntamim »

ive tried every single test case on the forum and that i can think of and all are correct

may i please get a hint or some help as to what im missing

i keep getting WA

Code: Select all

#include <stdio.h>
#include <iostream>
#include <queue>
#include <map>
#include <string>
#include <list>
#include <vector>
#include <math.h>

using namespace std;

char** matrix;
int eagles;
int n;

void explore(int i, int j)
{
	if(i<0 || j<0 || i>=n || j>=n)
		return;

	if(matrix[i][j]=='0')
		return;

	matrix[i][j] = '0';

	explore(i-1,j-1);
	explore(i+1,j-1);
	explore(i-1,j+1);
	explore(i+1,j+1);

	explore(i-1,j);
	explore(i+1,j);
	explore(i,j-1);
	explore(i,j+1);

}

int main()
{
	freopen("Bumble.in","r",stdin);  // Bumble.in

	int num=1;

	char temp[50];


	while(scanf("%d",&n)!=EOF)
	{
		matrix = new char*[n];
		for(int i=0 ; i<n ; i++)
		{
			matrix[i] = new char[n];
			scanf("%s",matrix[i]);
		}

		eagles = 0;
		for(int i=0 ; i<n ; i++)
		{
			for(int j=0 ; j<n ; j++)
			{
				if(matrix[i][j]=='1')
				{
					eagles++;
					explore(i,j);
				}
			}
		}

		printf("Image number %d contains %d war eagles.\n",num,eagles);
		num++;
	}
	return 0;
}
mntamim
New poster
Posts: 8
Joined: Wed Nov 06, 2013 12:08 am

Re: 352 The Seasonal War wrong answer ??

Post by mntamim »

wow this is soo stupid
ive been at this problem for 2 days
it clearly says in the problem the input should be from file "Bumble.in"
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 352 The Seasonal War wrong answer ??

Post by brianfry713 »

No matter what the problem statement says on this judge always read from stdin and write to stdout.
Check input and AC output for thousands of problems on uDebug!
shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

RE verdict for UVa 352 - The Seasonal War

Post by shikhorroy »

Where is the problem? Why am I getting RE? Help me please...

Code: Select all

#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<climits>

#include<iostream>
#include<iomanip>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<utility>
#include<sstream>
#include<bitset>
#include<algorithm>
using namespace std;

#define MAX 100
#define PI acos(-1.0)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef map<string, int> msi;
#define REP(i, b, e)\
    for(int i = int(b); i <= int(e); i++)
#define TRvi(c, it)\
    for(vi::iterator it = (c).begin(); it != (c).end(); ++it )
#define TRvii(c, it)\
    for(vii::iterator it = (c).begin(); it != (c).end(); ++it )
#define sf scanf
#define pf printf
#define si(x) sf("%d",&x)
#define in(x) cin>>x
#define out(x) cout<<(x)
#define ln length()
#define sz size()
#define clr clear()
#define pb push_back
#define mp make_pair
#define READ(f) freopen(f, "r", stdin)
#define mem(a,b) memset(a,b,sizeof(a))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define WHITE 0
#define BLACK 1
//int dx[]={1,0,-1,0};
//int dy[]={0,1,0,-1}; //4 Direction
//int dx[]={1,1,0,-1,-1,-1,0,1};
//int dy[]={0,1,1,1,0,-1,-1,-1};//8 direction
//int dx[]={2,1,-1,-2,-2,-1,1,2};
//int dy[]={1,2,2,1,-1,-2,-2,-1};//Knight Direction
vi adjList[MAX];
int isTook[MAX];
int nodes, edges;
int side, cs = 0;
bool yes(int x)//Weather within boundery or not.
{
    return (x > -1 && x < side);
}
void dfs_func(int src)
{
    isTook[src] = BLACK;
    TRvi(adjList[src], it)
    if(isTook[*it] == WHITE)
        dfs_func(*it);
}
void CLR(int n)
{
    REP(i, 0, n - 1) adjList[i].clr;
}
int main()
{
//    READ("in.txt");
    while(si(side) != EOF)
    {
        getchar();
        int node = 0;
        string str, tstr;
        map<pii, int> m;
        REP(i, 0, side - 1)
        {
            getline(cin, str);
            REP(j, 0, side - 1)
            {
                if(str[j] == '1')
                {
                    if(m.find(mp(i, j)) == m.end())
                        m[mp(i, j)] = node++;
                    if(yes(j - 1) && str[j - 1] == '1')//if front is a node and has '1'.
                    {
                        adjList[m[mp(i, j - 1)]].pb(m[mp(i, j)]);
                        adjList[m[mp(i, j)]].pb(m[mp(i, j - 1)]);
                    }
                    if(yes(i - 1))//if there is an upper row.
                    {
                        if(yes(j - 1) && tstr[j - 1] == '1')
                        {
                            adjList[m[mp(i - 1, j - 1)]].pb(m[mp(i, j)]);
                            adjList[m[mp(i, j)]].pb(m[mp(i - 1, j - 1)]);
                        }
                        if(tstr[j] == '1')
                        {
                            adjList[m[mp(i - 1, j)]].pb(m[mp(i, j)]);
                            adjList[m[mp(i, j)]].pb(m[mp(i - 1, j)]);
                        }
                        if(yes(j + 1) && tstr[j + 1] == '1')
                        {
                            adjList[m[mp(i - 1, j + 1)]].pb(m[mp(i, j)]);
                            adjList[m[mp(i, j)]].pb(m[mp(i - 1, j + 1)]);
                        }
                    }
                }
            }
            tstr = str;
        }
        int tWar = 0;
        REP(i, 0, node -1)isTook[i] = WHITE;
        REP(nd, 0, node - 1)
        {
            if(isTook[nd] == WHITE)
            {
                tWar++;
                dfs_func(nd);
            }
        }
        pf("Image number %d contains %d war eagles.\n", ++cs, tWar);
        CLR(node);
    }
    return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: RE verdict for UVa 352 - The Seasonal War

Post by brianfry713 »

Input:

Code: Select all

1
1
21
000101001000011100100
011011100100100000101
100010011010111000010
000001111010011000100
101100111110000110011
010010011001011000100
100011010101100001110
011110000000001100110
000001010011110110110
110000111110010110111
010001000111101010101
101010000101001111001
111110110101000001001
100111011110000011101
000010110110100001100
100110000001000001110
100101111101000000000
101111100110010101011
111010000111010100111
110100000101101101110
011001011000011111111
17
01110001111011001
10110101000111110
01110001011010111
10011110011001010
11011110100101000
11111100100010101
11001101001001000
11110110100001101
11101100011011100
00110011110010011
00000011000110110
00011111001010011
01100000111111111
10101101101110110
11000111000011110
11110011110100010
01100000000110000
15
001000010011001
101100100000000
011111110111011
101100110100101
000111000011100
110000010100100
001010111110000
010100100110110
010101110011110
101111000001010
110100101100110
110001001001111
000011001101011
000000101001000
100100000110111
5
11010
00011
11111
01011
00101
23
10000100101100011101111
10111100010111100110110
11010111010111010111000
01111011101101011111001
10001011100011100101111
00111100100101010000110
01101110111111110101110
11011001011000111000111
11111001111001001011010
10101010111000011100000
01011101000011000111111
01101101100011001010000
00111011000011100100010
01011011000010100100011
01011001110000011011110
01100101110010101000110
00001111110011000101010
10010100000010101001111
00101111001001011111010
11110010011100011001111
00011110001101000111101
01010101110001101110011
01000001011110001100111
2
01
11
11
00110110111
00011000100
10010101011
10010001111
00000101111
10100101000
01011100001
11101010110
11000000011
00010111000
10100111100
1
0
12
110101100001
011010100110
000001110110
100110010111
100011010000
111100111100
001010111110
110110111001
101011010111
110001110100
111001111011
010000111001
12
110010111110
000111101101
010010001000
011100110101
011001101010
011111111101
110010111111
010111010101
010100101100
010101110101
000010100110
100000100110
12
011100101101
001110000101
001111101011
111001111101
111101001110
100010101001
010101010110
010111001000
010110110110
010011010011
111111100010
001000100111
3
100
010
101
12
010010110011
010011101111
101011110111
101100001011
101010001110
001010000111
101000101100
101000000111
111000110011
111001010100
100101010110
001011100000
2
01
00
15
010010011011111
010110110111111
010001001000111
000111111000110
001001011110101
001011010110100
100100001010000
101110011000010
010001101001101
100011110010111
100000111001110
100111000001010
110011000100101
110101001001101
110011101111001
15
101000001001100
100111010111111
111010100001101
111101000011011
100001011100010
111101011010011
101110011001001
000110011111110
101010001000100
000011110000111
000111111011011
111100111111111
111100010011100
110000111010001
110000111111001
8
10100110
01101100
10111000
11110001
01110010
00111100
11001010
00101011
12
100110111110
110010101100
001010001000
010001010010
100010000101
000100111110
011101111111
101111010000
001010000110
001010100010
111100000101
001000000101
18
110100101001001011
010011001101000011
001011111110010011
111100010011100001
110100101111100011
010001011001111101
100111001011110100
100000011100000011
110111010000110100
101010010001010001
100111000010100001
011011110111101100
000101111000101010
100001110000110100
110010010111010010
101101110110000001
010000001000000110
010101101101100111
AC output:

Code: Select all

Image number 1 contains 1 war eagles.
Image number 2 contains 3 war eagles.
Image number 3 contains 5 war eagles.
Image number 4 contains 6 war eagles.
Image number 5 contains 2 war eagles.
Image number 6 contains 3 war eagles.
Image number 7 contains 1 war eagles.
Image number 8 contains 8 war eagles.
Image number 9 contains 0 war eagles.
Image number 10 contains 2 war eagles.
Image number 11 contains 3 war eagles.
Image number 12 contains 2 war eagles.
Image number 13 contains 1 war eagles.
Image number 14 contains 2 war eagles.
Image number 15 contains 1 war eagles.
Image number 16 contains 3 war eagles.
Image number 17 contains 3 war eagles.
Image number 18 contains 1 war eagles.
Image number 19 contains 5 war eagles.
Image number 20 contains 6 war eagles.
Check input and AC output for thousands of problems on uDebug!
shikhorroy
New poster
Posts: 27
Joined: Sat Jul 27, 2013 3:52 am

Re: RE verdict for UVa 352 - The Seasonal War

Post by shikhorroy »

Thanks....I got AC. :)
Post Reply

Return to “Volume 3 (300-399)”