Programming contests

50 Programming Exercise for Beginners

January 2, 2019 12:00 AM – December 31, 2019 12:00 AM

If There Is No Cat at Home, the Mice Squeak

Consider the following header file:

myheader.h

  1. #ifndef _MYHEADER_H
  2. #define _MYHEADER_H 1
  3. int foo(char *, int);
  4. #endif /* myheader.h */
download as text file

Write the function foo() declared in myheader.h that takes a string and a positive integer n as parameters. The string describes the wall of a room with mouse holes, where 'O' (uppercase letter O) indicates a mouse hole, '>' denotes a mouse looking to the right, '<' a mouse looking to the left, and '.' (period) characters denote the rest of the wall. At each end of the wall, in the corners of the room, there are mouse holes.

British scientists have proved that if mice smell a cat in the vicinity, they start running in the direction they are facing at the moment. Each mouse travels one unit per second, and as soon as they reach a mouse hole, they hide in there. If two mice meet along the wall while fleeing, they pass by each other neatly without time loss and run further.

The function should return true if a cat arriving after n seconds does not find any mice beside the wall, and false otherwise.

Place the function in file foo.c and submit this file as a solution to the evaluation system. You can test your solution using the following files. The evaluation system does not necessarily perform the evaluation using these files.

main.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "myheader.h"
  4. int main()
  5. {
  6.     char line[1000];
  7.     int n;
  8.     while (scanf("%s %d", line, &n) != EOF)
  9.         puts(foo(line, n) ? "YES" : "NO");
  10.     return EXIT_SUCCESS;
  11. }
download as text file

Makefile

  1. SRCS = main.c foo.c
  2. OBJS = $(SRCS:%.c=%.o)
  3. TARGETS = main
  4. .PHONY: clean
  5. all: $(TARGETS)
  6. main: $(OBJS)
  7. $(CC) $(OBJS) -o main
  8. %.o: %.c
  9. $(CC) -Wall -c $< -o $@
  10. clean:
  11. rm -rf $(OBJS) *~ $(TARGETS)
download as text file

Sample Input

  1. O..>.O...<..O 5
  2. O..>.<..O 3
  3. O..<.O 3
download as text file

Output for Sample Input

  1. YES
  2. NO
  3. YES
download as text file

One possible solution is available in: main.c.

Acknowledgement This work was supported by the construction EFOP-3.4.3-16-2016-00021. The project was supported by the European Union, co-financed by the European Social Fund.
University of Debrecen; Faculty of Informatics; v. 03/01/2019