Language: C++

// AOJ 0172: Doctor's Research Rooms
// 2018.1.24 bal4u@uu

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

#define MOVE 0x40
#define ON   0x50
#define OFF  0x60

#define QMAX 300000
typedef struct { char rm; int light; char step, act[50]; } Q;
Q q[QMAX+2]; int top, end;

int rm[15][15], s_rm[15];
int sw[15][15], s_sw[15];
char mk[15][33000];

char *msg[7] = {0,
"You can go home in %d steps.\n",
"You can not switch off all lights.",
"Help me!",
"Move to room %d.\n",
"Switch on room %d.\n",
"Switch off room %d.\n" };

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0;
	int c = getchar_unlocked();
	while (c < '0') c = getchar_unlocked();
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

int cmp(int *a, int *b) { return *a - *b; }

int main()
{
	int n, m, i, j, s, t, ans;
	int light;
	Q p, np;

	while (n = in()) {
		m = in();
		memset(s_rm, 0, sizeof(s_rm));
		memset(s_sw, 0, sizeof(s_sw));

		for (i = 0; i < m; i++){
			s = in()-1, t = in()-1;
			rm[s][s_rm[s]++] = t;
			rm[t][s_rm[t]++] = s;
		}

		light = 0; for (i = 0; i < n; i++) if (in()) light |= 1 << i;

		for (i = 0; i < n; i++) {
			j = in(); while (j--) sw[i][s_sw[i]++] = in()-1;
			qsort(sw[i], s_sw[i], sizeof(int), cmp);
        }

		memset(mk, 0, sizeof(mk));
		q[0].rm = 0, q[0].light = light, q[0].step = 0;
		top = 0, end = 1;
		ans = 3;

		while (top != end) {
			p = q[top]; if (++top == QMAX) top = 0;
			s = p.rm, t = p.light;

			if (mk[s][t]) continue;
			mk[s][t] = 1;
			if (s == n-1) {
				if (t == 1<<(n-1)) { ans = 1; break; }
				ans = 2;
			}

			for (i = 0; i < s_sw[s]; i++) if (sw[s][i] != s) {
				j = t ^ (1<<sw[s][i]);
				np = p, np.light = j;
				if (t & (1<<sw[s][i])) np.act[np.step] = OFF | sw[s][i];
				else 				   np.act[np.step] = ON  | sw[s][i];
				np.step++;
				q[end] = np; if (++end == QMAX) end = 0;
			}

			for (i = 0; i < s_rm[s]; i++) if (t & (1<<rm[s][i])) {
				np = p, np.rm = rm[s][i];
				np.act[np.step++] = MOVE | rm[s][i];
				q[end] = np; if (++end == QMAX) end = 0;
			}
		}
 
		if (ans > 1) puts(msg[ans]);
		else {
			printf(msg[ans], p.step);
			for (i = 0; i < p.step; i++) printf(msg[p.act[i] >> 4], 1+(p.act[i] & 0xf));
		}
    }
    return 0;
}


Compiler output:
a.cc:21:1: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   21 | "You can go home in %d steps.\n",
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:22:1: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   22 | "You can not switch off all lights.",
      | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:23:1: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   23 | "Help me!",
      | ^~~~~~~~~~
a.cc:24:1: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   24 | "Move to room %d.\n",
      | ^~~~~~~~~~~~~~~~~~~~
a.cc:25:1: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   25 | "Switch on room %d.\n",
      | ^~~~~~~~~~~~~~~~~~~~~~
a.cc:26:1: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
   26 | "Switch off room %d.\n" };
      | ^~~~~~~~~~~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:62:60: error: invalid conversion from 'int (*)(int*, int*)' to '__compar_fn_t' {aka 'int (*)(const void*, const void*)'} [-fpermissive]
   62 |                         qsort(sw[i], s_sw[i], sizeof(int), cmp);
      |                                                            ^~~
      |                                                            |
      |                                                            int (*)(int*, int*)
In file included from /usr/include/c++/14/cstdlib:79,
                 from /usr/include/c++/14/stdlib.h:36,
                 from a.cc:5:
/usr/include/stdlib.h:971:34: note:   initializing argument 4 of 'void qsort(void*, size_t, size_t, __compar_fn_t)'
  971 |                    __compar_fn_t __compar) __nonnull ((1, 4));
      |                    ~~~~~~~~~~~~~~^~~~~~~~