Skip to content

Instantly share code, notes, and snippets.

@caigen
Created October 12, 2014 05:49
Show Gist options
  • Save caigen/ae64e4183781ee92bb7d to your computer and use it in GitHub Desktop.
Save caigen/ae64e4183781ee92bb7d to your computer and use it in GitHub Desktop.
auto-timetabling
#include <string.h>
#include <stdio.h>
/*
x-axis: time id
y-axis: teacher/course id
0 1 2
-----------
0 | 1 1 -1
1 | 1 1 1
2 |-1 -1 1
*/
#define Y 3
#define X 3
int priority[Y][X] = {
1, 1, -1,
1, 1, 1,
-1, -1, 1
};
int result[Y][X];
void output() {
for (int j = 0; j < Y; ++j) {
for (int i = 0; i < X; ++i) {
printf("%d", result[j][i]);
if (i != X - 1) {
printf(" ");
}
}
printf("\n");
}
}
bool noconflict(int y, int x) {
// it is non-available time.
if (priority[y][x] == -1) {
return false;
}
// it is available time.
// is it confilct with previous decisions?
for (int j = 0; j < y; ++j) {
if (result[j][x] == 1) {
return false;
}
}
// ok.
return true;
}
void search(int y) {
if (y == 3) {
printf("get a result.\n");
output();
return;
}
for (int x = 0; x < X; ++x) {
if (noconflict(y, x)) {
// place.
result[y][x] = 1;
search(y+1);
// un-place
result[y][x] = 0;
}
}
}
int main(int argc, char* argv[]) {
search(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment