1 / 5
May 2020
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MAXSIZE 2500
struct queue{
    int index[MAXSIZE];
    int front;
    int rear;
};
int isEmpty(struct queue *order){
    if(order->front==-1)   
    return 1;
    
    return 0;
}
void init(struct queue *order){
    order->front=-1; 
    order->rear=-1;
}
void pop(struct queue *order){
    if(order->front==order->rear){
        order->front=-1; 
        order->rear=-1;
    }
    else if(order->front==MAXSIZE-1&&order->front!=order->rear){
        order->front=0; 
        order->rear%=MAXSIZE;
    }
    else order->front++;
}
void push(struct queue *order,int num){
    if(fabs(order->rear-order->front)==MAXSIZE-1)
    return;
    
    if(order->rear==-1)
    order->front++;
    
        order->rear++;
        order->index[order->rear%MAXSIZE]=num;
}
char input[110][110];
int main()
{
    struct queue order;
    init(&order);
    int choice,num,row,col,result;
    int testcase=1,r,c;
    char ch,str[51];
    while(1){
        int visited[110][110]={0};
        scanf("%d%d",&r,&c);
            if(r==0&&c==0)
            break;
        for(int i=0;i<r;i++){
        	scanf(" %s",str);
            for(int j=0;str[j]!='\0';j++){
                input[i][j]=str[j];
                if(input[i][j]=='A')
                push(&order,i*c+j);
            }
        }
            
        if(isEmpty(&order))
		{
			printf("Case %d: 0\n",testcase);
		}
		else
		{
			while(!isEmpty(&order))
			{
				row = order.index[order.front] / c;
				col = order.index[order.front] % c;
				ch = input[row][col];

				if(row+1 < r && input[row+1][col] == ch + 1)
				{
					if(visited[row+1][col] == 0)
					{
						visited[row+1][col] == 1;
						push(&order,(row+1)* c + col);

					}
				}

				if(row-1 >=0 && input[row-1][col] == ch + 1)
				{
					if(visited[row-1][col] == 0)
					{
						visited[row-1][col] == 1;
						push(&order,(row-1)* c + col);

					}
				}


				if(row+1 < r && col +1 < c && input[row+1][col+1] == ch + 1)
				{
					if(visited[row+1][col+1] == 0)
					{
						visited[row+1][col+1] == 1;
						push(&order,(row+1)* c + (col+1));

					}
				}


				if(row+1 < r && col-1 >= 0 &&  input[row+1][col-1] == ch + 1)
				{
					if(visited[row+1][col-1] == 0)
					{
						visited[row+1][col-1] == 1;
						push(&order,(row+1)* c + (col-1));

					}
				}


				if(row -1 >=0 && col +1 < c && input[row-1][col+1] == ch + 1)
				{
					if(visited[row-1][col+1] == 0)
					{
						visited[row-1][col+1] == 1;
						push(&order,(row-1)* c + (col+1));

					}
				}


				if(row -1 >=0 && col - 1 >= 0 && input[row-1][col-1] == ch + 1)
				{
					if(visited[row-1][col-1] == 0)
					{
						visited[row-1][col-1] == 1;
						push(&order,(row-1)* c + (col-1));

					}
				}


				if(col+1 < c && input[row][col+1] == ch + 1)
				{
					if(visited[row][col+1] == 0)
					{
						visited[row][col+1] == 1;
						push(&order,(row)* c + (col+1));

					}
				}


				if(col-1 >= 0 && input[row][col-1] == ch + 1)
				{
					if(visited[row][col-1] == 0)
					{
						visited[row][col-1] == 1;
						push(&order,(row)* c + (col-1));

					}
				}
				pop(&order);
			}
			result = ch - 'A' + 1;
			printf("Case %d: %d\n",testcase,result);
		}
		testcase++;
    }
    return 0;
    
}
  • created

    May '20
  • last reply

    May '20
  • 4

    replies

  • 639

    views

  • 3

    users

  • 1

    link

Please, don’t use a subject field for writing poetry or asking a question.

Sorry I’am new to this,Will not repeat this agian.
But,Can i know which testcases is my code failing.

I’m also not able, but please very carefully and with attention look for this two lines and find one error:

 if(visited[row][col-1] == 0)
 {
	visited[row][col-1] == 1;

The same errors are in many [I don’t tell you how many :wink: ] other places.