Code: Select all
#include "stdafx.h"
getchar();
Code: Select all
int n,onum,a[100],b[100],op[100],temp[30];
These, s1[4] and s2[4], cause strcmp to malfunction.
Code: Select all
int i,k,j=1;char s1[4],s2[4];
Moderator: Board moderators
Code: Select all
#include "stdafx.h"
getchar();
Code: Select all
int n,onum,a[100],b[100],op[100],temp[30];
Code: Select all
int i,k,j=1;char s1[4],s2[4];
TLE (Time Limit Exceeded) will be caused when your code did calc infinity or your algorithm was too much slow. I think that your algorithm isn't slow, so I propose you should check your code carefully again to avoid infinite calculation or wrong answer.gladiatorcn wrote:Why is it Time Limit Exceeded? No such problem did I ever meet when I post it to other online judge system, like Peking University Online Judge.
Help me, please!
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define _MOVE 1
#define _PILE 2
#define _ONTO 3
#define _OVER 4
#define INTTBL 100
#define NBWORDS 100
#define Y 100
// identify the meaning of a word in the command
int iden(char *str){
if(!strcmp(str, "move")) return _MOVE;
else if(!strcmp(str, "pile")) return _PILE;
else if(!strcmp(str, "over")) return _OVER;
else if(!strcmp(str, "onto")) return _ONTO;
else return 0;
}
// give each word of le command
void strintbl(char tblc[NBWORDS][Y], char *str){
int i=0,j=0,k=0;
while(str[i]!='\0'){
while((str[i]!=' ') && (str[i]!='\0')){
tblc[j][k]=str[i];
i++;
k++;
}
tblc[j][k]='\0';
k=0;
i++;
j++;
}
}
//initialize the string buffer
void inittbl(char tbl[NBWORDS][Y]){
int i;
for(i=0 ; i<NBWORDS; i++) tbl[i][0]='\0';
}
//remove the spaces after the command stocked in the buffer
void adaptestr(char *str){
int i=0;
while(str[i]!='\0')
i++;
i--;
while(str[i]==' '){
str[i]='\0';
i--;
}
}
//put in coord[x][y], the position of element a int the table
// x is the line and y the column
int searchnb(int tbl[INTTBL][INTTBL],int a, int coord[2]){
int i=0, j=0;
coord[0]=coord[1]=-1;
for(i=0; i<INTTBL; i++){
while(tbl[i][j]!=-1){
if(tbl[i][j]==a){
coord[0]=i;
coord[1]=j;
}
j++;
}
j=0;
}
}
// move function ... :)
void movenum(int tbl[INTTBL][INTTBL], int a, int b, int onto_or_over){
int i=0,j=0;
int coord[2];
if(onto_or_over==-1){
searchnb(tbl, a, coord);
if(coord[0]!=-1 && coord[1]!=-1){
tbl[coord[0]][coord[1]] = -1;
i=coord[1]+1;
while(tbl[coord[0]][i]!=-1){tbl[coord[0]][i-1]=tbl[coord[0]][i]; i++;}
tbl[coord[0]][i-1]=tbl[coord[0]][i];
i=0;
while(tbl[b][i]!=-1) i++;
tbl[b][i] = a;
}
}
else {
searchnb(tbl,a, coord);
if(coord[0]!=-1 && coord[1]!=-1){
tbl[coord[0]][coord[1]] = -1;
i=coord[1]+1;
while(tbl[coord[0]][i]!=-1){tbl[coord[0]][i-1]=tbl[coord[0]][i]; i++;}
tbl[coord[0]][i-1]=tbl[coord[0]][i];
i=0;
}
searchnb(tbl,b,coord);
if(coord[0]!=-1 && coord[1]!=-1){
i=coord[1]+1;
while(tbl[coord[0]][i]!=-1) i++;
tbl[coord[0]][i]=a;
}
}
}
// pile function ... :)
int pilenum(int tbl[INTTBL][INTTBL], int a , int b, int onto_or_over){
int coorda[2];
int coordb[2];
int i=0;
int j=0;
searchnb(tbl,a, coorda);
searchnb(tbl,b, coordb);
if(coorda[0]==coordb[0]) return 0;
i=coorda[1];
if(onto_or_over==-1){
while(tbl[b][j]!=-1)j++;
while(tbl[coorda[0]][i]!=-1){tbl[b][j]=tbl[coorda[0]][i]; i++; j++; }
}
else{
j=coordb[1]+1;
while(tbl[coorda[0]][i]!=-1){tbl[coordb[0]][j]=tbl[coorda[0]][i]; i++; j++; }
}
i=coorda[1];
while(tbl[coorda[0]][i]!=-1){tbl[coorda[0]][i]=-1; i++;}
return 0;
}
// similar to atoi() function.
int ToInt(char *str){
int p=1;
int nb=0;
int len=strlen(str);
int i=0;
i=len-1;
while(i>=0){
nb+= p*(str[i]-48);
i--;
p*=10;
}
return nb;
}
//execute the command
int exec(char tbl[NBWORDS][Y], int num[INTTBL][INTTBL],int i){
int onto_or_over=-1;
if((ToInt(&(tbl[1][0])) >= i) || (ToInt(&(tbl[3][0])) >= i) || (ToInt(&(tbl[1][0])) < 0) || (ToInt(&(tbl[3][0])) < 0)) return 0;
if(iden(&(tbl[2][0])) == _OVER) onto_or_over=1;
if(iden(&(tbl[0][0]))== _MOVE) movenum(num, ToInt(&(tbl[1][0])), ToInt(&(tbl[3][0])), onto_or_over);
else if(iden(&(tbl[0][0]))== _PILE) pilenum(num, ToInt(&(tbl[1][0])), ToInt(&(tbl[3][0])), onto_or_over);
return 0;
}
//put -1 everywhere in the table tbl to marque the end of a line of numbers.
void inittabnum(int tbl[INTTBL][INTTBL]){
int i,j;
for(i=0; i<INTTBL; i++)
for(j=0; j<INTTBL; j++)
tbl[i][j]=-1;
}
//equal to memset() function
void mem(char *str, char a, int b){
int i;
for(i=0; i<b; i++){
str[i]=a;
}
}
// last but not least. The main function :)
int main(){
int a,i=30,j=0;
int tbl[INTTBL][INTTBL];
char tblstr[NBWORDS][Y];
char str[Y];
inittabnum(tbl);
while(i>25)
scanf("%d", &i);
for(a=0; a<i; a++)
tbl[a][0]=a;
mem(str,'\0',Y);
gets(str);
gets(str);
while(strcmp(str, "quit")!=0){
adaptestr(str);
inittbl(tblstr);
strintbl(tblstr, str);
exec(tblstr, tbl,i);
mem(str,'\0',Y);
gets(str);
}
for(a=0; a<i; a++){
printf("%d:", a);
while(tbl[a][j]!=-1) {printf(" %d", tbl[a][j]); j++;}
printf("\n");
j=0;
}
}
5
move 1 onto 0
move 2 onto 0
move 3 onto 0
move 0 over 4
5
move 1 onto 0
0: 0 1
1:
2: 2
3: 3
4: 4
move 2 onto 0
0: 0 2
1: 1
2:
3: 3
4: 4
move 3 onto 0
0: 0 3
1: 1
2: 2
3:
4: 4
move 0 over 4
0:
1: 1
2: 2
3: 3
4: 4 0
output of each step10
pile 6 onto 5
pile 5 over 2
pile 1 over 7
move 9 onto 6
move 6 over 6
pile 8 onto 9
move 0 over 3
pile 5 onto 2
move 8 onto 7
pile 6 onto 2
pile 3 onto 9
move 7 over 4
pile 0 onto 6
pile 0 over 3
move 0 over 1
pile 5 over 7
move 5 over 9
pile 7 over 5
move 5 over 7
move 4 onto 0
quit
10
pile 6 onto 5
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5 6
6:
7: 7
8: 8
9: 9
-------------------------------------
pile 5 over 2
0: 0
1: 1
2: 2 5 6
3: 3
4: 4
5:
6:
7: 7
8: 8
9: 9
-------------------------------------
pile 1 over 7
0: 0
1:
2: 2 5 6
3: 3
4: 4
5:
6:
7: 7 1
8: 8
9: 9
-------------------------------------
move 9 onto 6
0: 0
1:
2: 2 5 6 9
3: 3
4: 4
5:
6:
7: 7 1
8: 8
9:
-------------------------------------
move 6 over 6
pile 8 onto 9
0: 0
1:
2: 2 5 6 9 8
3: 3
4: 4
5:
6:
7: 7 1
8:
9:
-------------------------------------
move 0 over 3
0:
1:
2: 2 5 6 9 8
3: 3 0
4: 4
5:
6:
7: 7 1
8:
9:
-------------------------------------
pile 5 onto 2
move 8 onto 7
0:
1: 1
2: 2 5 6 9
3: 3 0
4: 4
5:
6:
7: 7 8
8:
9:
-------------------------------------
pile 6 onto 2
pile 3 onto 9
0:
1: 1
2: 2 5 6 9 3 0
3:
4: 4
5:
6:
7: 7 8
8:
9:
-------------------------------------
move 7 over 4
0:
1: 1
2: 2 5 6 9 3 0
3:
4: 4 7
5:
6:
7:
8: 8
9:
-------------------------------------
pile 0 onto 6
pile 0 over 3
move 0 over 1
0:
1: 1 0
2: 2 5 6 9 3
3:
4: 4 7
5:
6:
7:
8: 8
9:
-------------------------------------
pile 5 over 7
0:
1: 1 0
2: 2
3:
4: 4 7 5 6 9 3
5:
6:
7:
8: 8
9:
-------------------------------------
move 5 over 9
pile 7 over 5
move 5 over 7
move 4 onto 0
0:
1: 1 0 4
2: 2
3: 3
4:
5: 5
6: 6
7: 7
8: 8
9: 9
-------------------------------------
output20
pile 6 onto 15
pile 15 over 12
pile 1 over 7
move 19 onto 6
move 6 over 16
pile 8 onto 9
move 10 over 3
pile 15 onto 2
move 18 onto 7
pile 16 onto 2
pile 13 onto 19
move 17 over 4
pile 10 onto 6
pile 0 over 13
move 10 over 1
pile 5 over 7
move 5 over 9
pile 17 over 15
move 5 over 7
move 4 onto 10
pile 8 over 18
move 4 onto 11
move 19 over 0
move 8 onto 12
move 6 over 19
pile 10 over 18
pile 1 onto 2
pile 12 over 16
pile 0 over 1
pile 9 over 19
quit
and, not printed by some commands are ignored.20
pile 6 onto 15
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6:
7: 7
8: 8
9: 9
10: 10
11: 11
12: 12
13: 13
14: 14
15: 15 6
16: 16
17: 17
18: 18
19: 19
-------------------------------------
pile 15 over 12
0: 0
1: 1
2: 2
3: 3
4: 4
5: 5
6:
7: 7
8: 8
9: 9
10: 10
11: 11
12: 12 15 6
13: 13
14: 14
15:
16: 16
17: 17
18: 18
19: 19
-------------------------------------
pile 1 over 7
0: 0
1:
2: 2
3: 3
4: 4
5: 5
6:
7: 7 1
8: 8
9: 9
10: 10
11: 11
12: 12 15 6
13: 13
14: 14
15:
16: 16
17: 17
18: 18
19: 19
-------------------------------------
move 19 onto 6
0: 0
1:
2: 2
3: 3
4: 4
5: 5
6:
7: 7 1
8: 8
9: 9
10: 10
11: 11
12: 12 15 6 19
13: 13
14: 14
15:
16: 16
17: 17
18: 18
19:
-------------------------------------
move 6 over 16
0: 0
1:
2: 2
3: 3
4: 4
5: 5
6:
7: 7 1
8: 8
9: 9
10: 10
11: 11
12: 12 15
13: 13
14: 14
15:
16: 16 6
17: 17
18: 18
19: 19
-------------------------------------
pile 8 onto 9
0: 0
1:
2: 2
3: 3
4: 4
5: 5
6:
7: 7 1
8:
9: 9 8
10: 10
11: 11
12: 12 15
13: 13
14: 14
15:
16: 16 6
17: 17
18: 18
19: 19
-------------------------------------
move 10 over 3
0: 0
1:
2: 2
3: 3 10
4: 4
5: 5
6:
7: 7 1
8:
9: 9 8
10:
11: 11
12: 12 15
13: 13
14: 14
15:
16: 16 6
17: 17
18: 18
19: 19
-------------------------------------
pile 15 onto 2
0: 0
1:
2: 2 15
3: 3 10
4: 4
5: 5
6:
7: 7 1
8:
9: 9 8
10:
11: 11
12: 12
13: 13
14: 14
15:
16: 16 6
17: 17
18: 18
19: 19
-------------------------------------
move 18 onto 7
0: 0
1: 1
2: 2 15
3: 3 10
4: 4
5: 5
6:
7: 7 18
8:
9: 9 8
10:
11: 11
12: 12
13: 13
14: 14
15:
16: 16 6
17: 17
18:
19: 19
-------------------------------------
pile 16 onto 2
0: 0
1: 1
2: 2 16 6
3: 3 10
4: 4
5: 5
6:
7: 7 18
8:
9: 9 8
10:
11: 11
12: 12
13: 13
14: 14
15: 15
16:
17: 17
18:
19: 19
-------------------------------------
pile 13 onto 19
0: 0
1: 1
2: 2 16 6
3: 3 10
4: 4
5: 5
6:
7: 7 18
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15
16:
17: 17
18:
19: 19 13
-------------------------------------
move 17 over 4
0: 0
1: 1
2: 2 16 6
3: 3 10
4: 4 17
5: 5
6:
7: 7 18
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15
16:
17:
18:
19: 19 13
-------------------------------------
pile 10 onto 6
0: 0
1: 1
2: 2 16 6 10
3: 3
4: 4 17
5: 5
6:
7: 7 18
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15
16:
17:
18:
19: 19 13
-------------------------------------
pile 0 over 13
0:
1: 1
2: 2 16 6 10
3: 3
4: 4 17
5: 5
6:
7: 7 18
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15
16:
17:
18:
19: 19 13 0
-------------------------------------
move 10 over 1
0:
1: 1 10
2: 2 16 6
3: 3
4: 4 17
5: 5
6:
7: 7 18
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15
16:
17:
18:
19: 19 13 0
-------------------------------------
pile 5 over 7
0:
1: 1 10
2: 2 16 6
3: 3
4: 4 17
5:
6:
7: 7 18 5
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15
16:
17:
18:
19: 19 13 0
-------------------------------------
move 5 over 9
0:
1: 1 10
2: 2 16 6
3: 3
4: 4 17
5:
6:
7: 7 18
8:
9: 9 8 5
10:
11: 11
12: 12
13:
14: 14
15: 15
16:
17:
18:
19: 19 13 0
-------------------------------------
pile 17 over 15
0:
1: 1 10
2: 2 16 6
3: 3
4: 4
5:
6:
7: 7 18
8:
9: 9 8 5
10:
11: 11
12: 12
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0
-------------------------------------
move 5 over 7
0:
1: 1 10
2: 2 16 6
3: 3
4: 4
5:
6:
7: 7 18 5
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0
-------------------------------------
move 4 onto 10
0:
1: 1 10 4
2: 2 16 6
3: 3
4:
5:
6:
7: 7 18 5
8:
9: 9 8
10:
11: 11
12: 12
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0
-------------------------------------
pile 8 over 18
0:
1: 1 10 4
2: 2 16 6
3: 3
4:
5:
6:
7: 7 18 5 8
8:
9: 9
10:
11: 11
12: 12
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0
-------------------------------------
move 4 onto 11
0:
1: 1 10
2: 2 16 6
3: 3
4:
5:
6:
7: 7 18 5 8
8:
9: 9
10:
11: 11 4
12: 12
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0
-------------------------------------
move 19 over 0
move 8 onto 12
0:
1: 1 10
2: 2 16 6
3: 3
4:
5:
6:
7: 7 18 5
8:
9: 9
10:
11: 11 4
12: 12 8
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0
-------------------------------------
move 6 over 19
0:
1: 1 10
2: 2 16
3: 3
4:
5:
6:
7: 7 18 5
8:
9: 9
10:
11: 11 4
12: 12 8
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0 6
-------------------------------------
pile 10 over 18
0:
1: 1
2: 2 16
3: 3
4:
5:
6:
7: 7 18 5 10
8:
9: 9
10:
11: 11 4
12: 12 8
13:
14: 14
15: 15 17
16:
17:
18:
19: 19 13 0 6
-------------------------------------
pile 1 onto 2
0:
1:
2: 2 1
3: 3
4:
5:
6:
7: 7 18 5 10
8:
9: 9
10:
11: 11 4
12: 12 8
13:
14: 14
15: 15 17
16: 16
17:
18:
19: 19 13 0 6
-------------------------------------
pile 12 over 16
0:
1:
2: 2 1
3: 3
4:
5:
6:
7: 7 18 5 10
8:
9: 9
10:
11: 11 4
12:
13:
14: 14
15: 15 17
16: 16 12 8
17:
18:
19: 19 13 0 6
-------------------------------------
pile 0 over 1
0:
1:
2: 2 1 0 6
3: 3
4:
5:
6:
7: 7 18 5 10
8:
9: 9
10:
11: 11 4
12:
13:
14: 14
15: 15 17
16: 16 12 8
17:
18:
19: 19 13
-------------------------------------
pile 9 over 19
0:
1:
2: 2 1 0 6
3: 3
4:
5:
6:
7: 7 18 5 10
8:
9:
10:
11: 11 4
12:
13:
14: 14
15: 15 17
16: 16 12 8
17:
18:
19: 19 13 9
-------------------------------------
so...here's a part of my code responsible for printingThere should be one line of output for each block position (i.e., n lines of output where n is the integer on the first line of input).
Code: Select all
for(int i = 0; i < n; i++)
{
if(i)printf("\n");
printf("%2d:",i);int j = 0;
while(tab[i][j]>=0)
printf(" %d",tab[i][j++]);
}
Code: Select all
0: 0
1: 1 9 2 4
2:
3: 3
4:
5: 5 8 7 6
6:
7:
8:
9:
Code: Select all
printf("%2d:",i);
Code: Select all
printf("%d:",i);
Code: Select all
for(int i = 0; i < n; i++)
{
printf("%d:",i);int j = 0;
while(tab[i][j]>=0)
printf(" %d",tab[i][j++]);
printf("\n");
}
I think I have now one more line of output, but anyway...thanks a lot!There should be one line of output for each block position (i.e., n lines of output where n is the integer on the first line of input).